如何将八位字节流保存到 javascript 中压缩?
how to save octet stream to zip in javascript?
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();
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();