gpt4 book ai didi

Javascript:如何通过 Firefox 浏览器从 API 服务器下载 zip?

转载 作者:行者123 更新时间:2023-12-03 05:52:17 24 4
gpt4 key购买 nike

目前,我已经实现了以下内容,并且它可以在 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 {大小:504188,类型:“”}

最佳答案

可能是因为链接元素没有附加到正文吗?

当我尝试以下操作时,它在 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 中也能工作。

关于Javascript:如何通过 Firefox 浏览器从 API 服务器下载 zip?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40098062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com