简体   繁体   English

如何使用post api从reactjs下载ZIP文件?

[英]How to download ZIP file from reactjs using post api?

How to download zip file from reactjs using POST API.如何使用 POST API 从 reactjs 下载 zip 文件。 The request is coming from nodejs in binary form请求以二进制形式来自 nodejs

you can use jszip link https://github.com/Stuk/jszip like您可以使用 jszip 链接https://github.com/Stuk/jszip 之类的

import zipTargetFiles from '/path'

zipTargetFiles( data ).then(file => {
 //operations
})

if you use fetch like this.如果你像这样使用 fetch。

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)
})
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]));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM