Varun verma
Varun verma

Reputation: 189

How to download ZIP file from reactjs using post api?

How to download zip file from reactjs using POST API. The request is coming from nodejs in binary form

Upvotes: 0

Views: 8971

Answers (2)

Ashok
Ashok

Reputation: 753

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

Upvotes: 1

errorau
errorau

Reputation: 2334

you can use jszip link https://github.com/Stuk/jszip like

import zipTargetFiles from '/path'

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

if you use fetch like this.

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

Upvotes: 1

Related Questions