Ve promise download file
Code Revisions 2 Stars 8 Forks 2. Embed What would you like to do? Embed Embed this gist in your website. Share Copy sharable link for this gist. Learn more about clone URLs. Download ZIP.
Download File using axios : Node. This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
There is also a finally callback attached to this promise that we use for sharing code between the success and failure. Pay attention that each step towards completion of the promise seems to be handled in an async manner and the actual asynchronicity is handled by the promise mechanism and the service.
Your email address will not be published. Lets get right into it and see how we can actually do this and then I will explain you the actual process behind it if you want to know more. The previous snippet is basically a Promisified version of the downloading of an item from anywhere on the web. I like to work with Promises because I can use Async and Await on them which makes my code more clean and working as I need it to.
I am easily doing this by attaching a resolve and reject on the actual events that are coming from the request library. Whenever an object URL is created, it stays around for the lifetime of the document on which it was created. Usually, the browser will release all object URLs when the document is being unloaded. However, it is important that you release object URLs whenever they are no longer needed in order to improve performance and minimize memory usage.
It takes the object URL to be released as its argument. For example:. We have also seen how we can programmatically extract or generate content in the browser using Web APIs. In this section, we will examine how we can download programmatically generate content in the browser, leveraging all we have learned from the beginning of the article and what we already know about blobs and object URLs.
The logic of our helper function can be broken down as follows:. Here is what an implementation of this helper function will look like:. That was a pretty straightforward implementation of the download link helper function.
Notice that the helper triggers a one-off automatic download of the blob content whenever it is called. Also notice that the helper function takes a filename as its second argument, which is very useful for setting the default filename for the downloaded file. Now that we have our download helper function in place, we can revisit our previous examples and modify them to trigger a download for the generated content.
Here we go. We will update the final promise. Here we have updated the final promise. Here is a working and more advanced example of this application on Codepen :.
We will add some code to the end of the load event listener of the img object, to allow us:. Here is a working example of this application on Codepen :. While there could be a lot to pick from this tutorial, it is glaring that Web APIs have a lot to offer as regards building powerful apps for the browser.
Thanks for making out time to read this article. Great article!