Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to save downloaded binary data without using pipe #2518

Closed
Aranir opened this issue Jan 19, 2017 · 3 comments
Closed

How to save downloaded binary data without using pipe #2518

Aranir opened this issue Jan 19, 2017 · 3 comments

Comments

@Aranir
Copy link

Aranir commented Jan 19, 2017

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?

@XuJinNet
Copy link

The same problem.

@lambdacerro
Copy link

use encoding: null option

@Aranir
Copy link
Author

Aranir commented Aug 8, 2017

@lambdacerro Thank you, will close this issue then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants