目前,我已经实现了以下功能,并且可以在Chrome浏览器上使用。但是在Firefox浏览器上,它从API服务器获得了响应,但是没有任何内容下载到Firefox浏览器。
我可能做错了什么?以下不是跨平台兼容的吗?
先感谢您
这是代码:
var config = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(...)
}
fetch("https://test-server.com:8080/download/zip", config)
.then(response => response.blob())
.then(zipFile => {
console.log(zipFile)
var blob = zipFile;
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = 'download'
link.click();
})
.catch((error) => {
console.log("Error: ", error)
})
在Chrome上,
console.log(zipFile)
会记录以下内容:Blob {size: 504188, type: "application/zip"}
,但是在Firefox上,它会记录Blob {size: 504188, type: "" }
。 最佳答案
可能是因为link元素未附加到主体上吗?
当我尝试以下操作时,它可以在Chrome中工作,但不能在Firefox中工作(就像您遇到的那样):
link = document.createElement('a')
link.href = 'http://google.com'
link.click()
但
link = document.createElement('a')
link.href = 'http://google.com'
document.body.appendChild(link)
link.click()
也在Firefox中工作。