1

I need to save a response to a file. The response is a zip file returned from a server which is recieved as a blob. I need to save to blob as a zip file on my local machine. The application itself is Electron, and the file needs to be stored in the background (without bothering the user). Filetype is zip ( and needs to be unzipped after this.

const writer = fs.createWriteStream(path.join(dir, 'files.zip'));
                writer.on('pipe', (src) => {
                  console.log('Something is piping into the writer.');
                });
                writer.end('This is the end\n');
                writer.on('finish', () => {
                  console.log('All writes are now complete.');
                });

writer.pipe(new Blob([response.data]));

Best i can do gives a 1kb corrupt file. I have read the node docs, but i can't get it to work.

ANY response is greatly appreciated, and please elaborate if you can. I feel like i need to use some type of buffer.

4
  • 1
    What is the http server module that you using? Commented Feb 21, 2019 at 11:06
  • I'm getting a response.data trough an Axios response.Adding on; I am able to do two other things ( related) use a 'save as' function where the user saves it . and use the JSZip librabry to re-zip and save the file. So the recieved data is correct. So in short; Axios Commented Feb 21, 2019 at 11:09
  • 1
    Im sorry if I misunderstood. As of axios module that I found it is a client version. Are you using axios in server side? Commented Feb 21, 2019 at 11:13
  • No this is client side (Electron, VueJS, Axios). The server is plain PHP which returns the zip file. If you need any information please let me know. Commented Feb 21, 2019 at 11:15

2 Answers 2

1

Try this,

var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";

var url = window.URL.createObjectURL(blob);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
Sign up to request clarification or add additional context in comments.

7 Comments

I tried it, including some tinkering with the response and location. But there is no output. I do however get a deprecation warning DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
Thank you. I have tried it, and now there is no response whatsoever. No error or succes output. I have tried console logging around every statement ( the medieval way, and it seems the code doesnt get passed the new Buffer statement.
thats odd. Can run console.log(response.data) and console.log(typeof response.data) and comment the output
Ofcourse : console.log(response.data) produces object. And the typeof produces Blob(146757) {size: 146757, type: "application/octet-stream"}
Seems like you are dealing with browser Blob, so node fs module won't help. Try the above code and let me know what is the state.
|
0

So i figured finally it out.

Instead of a return type of Blob i had to return an arraybuffer. Next step is using the JSzip library and convert my function to async.

Final result:

//Create JSZip object to read incoming blob
const zip = new JSZip;

try {
  //Await unpacked arrayBuffer
  const zippedFiles = (await zip.loadAsync(response.data, { createFolders: true })).files;

  for (const file of Object.values(zippedFiles)) {
    if (file.dir) {
      await mkdirAsync(path.join(dir, file.name), { recursive: true });
    } else {
        file
          .nodeStream()
          .pipe(fs.createWriteStream(path.join(dir, file.name)))
          .on("finish", console.log);
      }
    }
  } catch (e) {
   throw MeaningfulError;
 }

In short: This function takes an arraybuffer (of type .zip) and unpacks it to your system.

  • response.data is the arraybuffer (which needs to be unpacked).
  • dir is the directory in which the content needs to be unpacked.

And that's all you need!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.