admin管理员组文章数量:1278910
I'm trying to automatically download a blob like this:
blobGeneratingFunction.then(blob => {
// blob => Blob(3797539) {size: 3797539, type: "image/png"}
let file = new Blob([blob], { type: 'application/octet-stream' })
file.name = 'test.png'
file.download = 'test.png'
let blobURL = URL.createObjectURL(file)
window.location.href = blobURL
})
Neither the name
or download
attribute managed to set the filename, which right now is:
f486177d-6f5e-4f96-91a9-8df08e7d9da0
How to set the filename propertly?
I'm trying to automatically download a blob like this:
blobGeneratingFunction.then(blob => {
// blob => Blob(3797539) {size: 3797539, type: "image/png"}
let file = new Blob([blob], { type: 'application/octet-stream' })
file.name = 'test.png'
file.download = 'test.png'
let blobURL = URL.createObjectURL(file)
window.location.href = blobURL
})
Neither the name
or download
attribute managed to set the filename, which right now is:
f486177d-6f5e-4f96-91a9-8df08e7d9da0
How to set the filename propertly?
Share Improve this question asked Jan 22, 2018 at 2:45 alexalex 7,61115 gold badges53 silver badges79 bronze badges1 Answer
Reset to default 9Blob
does not have a name
attribute.
Preserve the original type
, and use <a>
element with download
attribute with href
set to to the Blob URL
, call .click()
on <a>
element after appending the element to document.body
.
blobGeneratingFunction.then(blob => {
let a = document.createElement("a")
let blobURL = URL.createObjectURL(blob)
a.download = 'test.png'
a.href = blobURL
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
})
本文标签: javascriptCan39t set filename of BlobStack Overflow
版权声明:本文标题:javascript - Can't set filename of Blob - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741277060a2369789.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论