javascript - 如何在javascript中将八位字节流保存为zip?
问题描述
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)
像这样读取这个流。在价值上我有 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();
推荐阅读
- python - Pandas,在整行上应用两个函数的最有效方法
- c++ - RTSP 音频反向通道 (ONVIF) - 如何实现
- getstream-io - 如何将自定义 Header 添加到 Stream Feed Activity?
- drupal - 如何在块树枝模板中显示当前页面的字段
- sql - 如何在sql中具有一定数量的重复项的范围之间创建一个随机数
- python - 使用汤,find_all 返回一个 [ ] 值
- python - 训练 Keras 模型期间的 GPU 使用率为 1%
- javascript - pdfmake中分页符处的动态副标题问题
- python - 如何在 Excel 中使用“FindNext”?
- c# - 从周期性异步请求中创建 observable