question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to save downloaded binary data without using pipe

See original GitHub issue

On my node server and try to download a png image from a url and write it to a file:

  request
.get("https://url/for/png/file")
.on('error', function(err) {
  console.log(err)
})
.on('response', function(response){
  console.log(response.statusCode) // 200
  console.log(response.headers['content-type']); // 'image/png'
})
.on('complete', (resp: http.IncomingMessage, body: string | Buffer) => {
  fs.writeFile(".../test.png", body, 'binary');

});

The main issue is that the written file is corrupted (can’t be opened).

If I use pipe instead of on(‘complete’…) at the end the written file is correct.

.pipe(fs.createWriteStream(".../test.png"))

What exactly is the difference and what am I missing to be able to write the file?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

142reactions
L11Rcommented, May 15, 2017

@Aranir it’s too late maybe, but here is my solution:

const options = {
  url: 'https://url/for/png/file',
  encoding: null
};

request.get(options)
  .then(function (res) {
    const buffer = Buffer.from(res, 'utf8');
    fs.writeFileSync('/some/path', buffer);
  });
28reactions
rainecommented, Dec 27, 2017

^

Buffer.from(res, 'utf8') is not necessary since passing encoding: null as option would already make res a Buffer.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to download a file with Node.js (without using third- ...
"I only want to download a file from a given url, and then save it to a given directory," it seems pretty clear....
Read more >
Passing binary data to curl without using a @file
You can pass data into curl via STDIN like so: echo -e '...data...\n' | curl -X POST --data-binary @- http://foo.com. The @- tells...
Read more >
Downloading Files with cURL, Including Text and Binary ...
How to use cURL to download a file, including text and binary files.
Read more >
PowerShell's Object Pipeline Corrupts Piped Binary Data
Yesterday I used curl to download a huge database backup from a remote server. Curl is UNIX-ey. By default, it streams its output...
Read more >
Downloading files with curl
The curl tool lets us fetch a given URL from the command-line. Sometimes we want to save a web file to our own...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found