Fs stream download file
Get your weekly push notification about new and trending Future Studio content and recent platform enhancements. Marcus is a fullstack JS developer. Axios Series Overview. Prev Next. Twitter Facebook. Explore the Library Find interesting tutorials and solutions for your problems. If you use fs. Use fs. To download files using curl in Node.
We are using spawn instead of exec for the sake of convenience - spawn returns a stream with data event and doesn't have buffer size issue unlike exec. That doesn't mean exec is inferior to spawn ; in fact we will use exec to download files using wget. The way data was written to the instance of fs. The only difference is that the data and end events are listened on the stdout object of spawn.
Also we listen to spawn 's exit event to make note of any errors. Although it says downloading using wget , this example applies to downloading using curl with the -O option too. Was looking for this forever — Harrison Cramer.
This answer is not complete, because when you download some larger files, the pipe will give you more than one events. This code does not wait until the whole file has been downloaded before one can call then on it. Take a look at my solution to find what I consider a more complete solution. Armand Armand 1 1 silver badge 3 3 bronze badges. Welcome to StackOverflow!
You may want to provide a bit of explanation to go along with your code sample. Alexey Prokhorov 2, 1 1 gold badge 17 17 silver badges 23 23 bronze badges.
Aman Saraf Aman Saraf 2 2 silver badges 12 12 bronze badges. Why response type blob and not stream? I get "stream. Try setting your encoding to null and skip encoding the received data: fs.
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. Besides, I want to add my situation and solution Situation: using koa to develop a node.
A Got stream is a duplex stream , which means it is both readable and writable. For the purposes of downloading a file, we will just be using its readable properties. To download a file we need to send the response to the file system somehow. Streams allow you to pipe the data from one stream to another. The above code will download the file as long as there are no problems. If an error occurs the code will crash with an unhandled exception. There is also no feedback, so if your file is large you will not see any result until the download is complete.
We can listen to events on the stream to handle both of these cases.
0コメント