如何使用POST API从reactjs下载zip文件。 该请求以二进制形式来自nodejs
答案 0 :(得分:1)
您可以像
一样使用jszip链接https://github.com/Stuk/jszipimport zipTargetFiles from '/path'
zipTargetFiles( data ).then(file => {
//operations
})
如果您使用这样的访存方式。
fetch('URL', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
//Body
})
}).then((response)=>{
//here is youu want zip data
var zip = new JSZip();
var zipData = response.data //
// Add an top-level, arbitrary text file with contents
zip.file("response.txt", zipData);
// Generate the zip file asynchronously
zip.generateAsync({type:"blob"})
.then(function(content) {
// Force down of the Zip file
saveAs(content, "zipFile.zip");
});
}).catch((error)=>{
console.log(error)
})
答案 1 :(得分:0)
You can use JsZip on Client Side.
Then, do a request with axios. Like this:
request = (currentUrl: string): Promise<void> => axios({
url: currentUrl,
method: 'GET',
responseType: 'blob',
}).then((response) => {
const url: string = window.URL.createObjectURL(new Blob([response.data]));
});