gpt4 book ai didi

javascript - 如何在javascript中将八位字节流保存为zip?

转载 作者:太空宇宙 更新时间:2023-11-04 00:05:36 26 4
gpt4 key购买 nike

return new Promise((resolve) => {
return fetch('http:\\localhost:8080\downloadzipurl', {
method: 'POST',
headers: {
Authorization: authKey,
'Content-Type': 'application/json',
'cache-control': 'no-cache',
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify({
userName,
providerId,
sfgFEVersion,
}),
})
.then((resp) => {
if (resp.status >= 200 && resp.status < 300) {
const reader = resp.body.getReader();
const pump = () => reader.read().then((value, done) => {
if (done) {
//writer.close();
console.log('Api returned null response');
} else {
console.log(value);
const blob = new Blob([value], { type: 'application/zip'});
const fileName = 'QCPReport.zip';
FileSaver.saveAs(blob, fileName);
}
});

在上面的代码中,我得到了八位字节流格式的响应,正在读取此流reader.read().then((value, done),如下所示。在 value 中,我有 Uint8Array。如何将此值保存为 zip 文件?

最佳答案

你可以这样做:

const blob = new Blob([value]);

const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = fileName;
link.target = '_blank';
link.setAttribute("type", "hidden");

// This is needed for firefox
document.body.appendChild(link);

link.click();
link.remove();

关于javascript - 如何在javascript中将八位字节流保存为zip?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52443736/

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