Gloomy Gloomy - 6 months ago 19
Node.js Question

How do I save a downloaded file to the file system? (node.js)

I am trying to make a small scraper with node (electron) for learning purposes. I am stuck at trying to download files from the webpage.

For now I do :



fetch(fileUrl).then(function(response){
return response.arrayBuffer();
}).then(function(buffer){
var buff = new Int32Array(buffer);
fsp.writeFile("filename.pdf",buff).then(function(){console.log('Success!')})
})





But the fs part is wrong - I just can't figure out how to make it right. How do I know what sort of data (uint8, int32, etc.) I should use? I'm really confused about how this should work.

Answer

Assuming you're running Electron v0.37.5 or later, I think this should do the trick:

fetch(fileUrl).then(response => {
  var buff = Buffer.from(response.arrayBuffer());
  fsp.writeFile("filename.pdf", buff).then(() => {
    console.log('Success!')
  });
});
Comments