How to save downloaded binary data without using pipe
See original GitHub issueOn 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:
- Created 7 years ago
- Reactions:2
- Comments:7 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@Aranir it’s too late maybe, but here is my solution:
^
Buffer.from(res, 'utf8')
is not necessary since passingencoding: null
as option would already makeres
aBuffer
.