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

In above code am getting a response in octet-stream format am reading this stream reader.read().then((value, done) like this. in value i have Uint8Array. how can I save this value as a zip file?

  • Possible duplicate of [ArrayBuffer to blob conversion](https://stackoverflow.com/questions/44147912/arraybuffer-to-blob-conversion) – ponury-kostek Sep 21 '18 at 12:28
  • https://stackoverflow.com/questions/31127849/how-to-save-binary-data-of-zip-file-in-javascript/31172730#31172730 The solution given here worked for me but in a slightly different way through fetch – Vamshidhar Reddy Enumula Sep 24 '18 at 05:18

1 Answers1

2

You can do something like this:

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();
F.bernal
  • 2,594
  • 2
  • 22
  • 27