我正在尝试自动下载这样的 blob:
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
})
name
或 download
属性都无法设置文件名,现在是:
f486177d-6f5e-4f96-91a9-8df08e7d9da0
如何正确设置文件名?
最佳答案
Blob
没有 name
属性。
保留原件type
,并使用<a>
元素为 download
属性为 href
设置为Blob URL
,调用.click()
上<a>
将元素附加到 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)
})
关于javascript - 无法设置 Blob 的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48374038/