Don't complicate yourself with any Server-Side code, or third-party libraries anymore. I want to create a new button on the screen that when selected will download the url string inside songPath, instead of just opening it and playing it in the browser. The click () method prompts the download process to start as you click the download button. The encodeURIComponent () function then encodes the content. Now, on button click, you'll have the configurations file downloaded, and all of this just with the help of JavaScript Īlways use this trick when you need to download any files in the Browser. The declared function sets a download attribute where you get to name the file. Auto click on a element, trigger the file download Let's create the Download ButtonĪs we already know what's a Blob, let's have a look at how to download any data just using JavaScript Blob API.Ĭonsider having a JSON format object which contains some application configurations: const configuration = Ĭonst blobUrl = URL.createObjectURL(blobConfig) Ĭonst anchor = document.createElement('a') Īnchor.download = "my-configurations.json" The Fileinterface is based on Blob, inheriting blob functionality and expanding it to support files on the user's system. Blobs can represent data that isn't necessarily in a JavaScript-native format. MDN says: The Blob object represents a blob, which is a file-like object of immutable, raw data they can be read as text or binary data or converted into a ReadableStream so its methods can be used for processing the data. Thanks to Blob API, nowadays we can download any file (clicking on button) with JS more efficiently and painlessly ) What's a Blob? When you're used to be working only on JavaScript and sometimes needed to keep some data on your computer, you might have to implement some server-side code or some tricky HTML which weren't working all times.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |