Javascript download blob text file

Javascript download blob text file

javascript download blob text file

we need to create a Blob object and pass to it a string that contains the content of the file so if we want to download a Ruby code we can create a javascript file. You can prepare a text file for download after it is created using JavaScript and in this for creating a Blob object that held the data corresponding to diseinuak4web.net file. Download, - File Change Trigger, - Move File, - Path Exists, - Read Text File Go Home, - Inject JS Script, - Navigate To, - Open Browser, - Refresh Browser.

Javascript download blob text file - join told

Download file on button click in the Browser with Javascript

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. Thanks to Blob API, nowadays we can download any file (clicking on button) with JS more efficiently and painlessly ;)

What's a Blob?

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.
Blobs can represent data that isn't necessarily in a JavaScript-native format. The interface is based on , inheriting blob functionality and expanding it to support files on the user's system.

Read more about Blob on MDN.

Let's create the Download Button

As we already know what's a Blob, let's have a look at how to download any data just using JavaScript Blob API.

Consider having a JSON format object which contains some application configurations:

Firstly we have to convert out object into a :

After we need to create a  link using method and sending as parameter,  it's dynamic and always different:

As we have a URL, we just simply create an element with corresponding attribute:

Keep in mind, always do when you don't need that URL anymore. This is very important for performance.

That's it! Here's the full code:

Now, on button click, you'll have the configurations file downloaded, and all of this just with the help of JavaScript;

Easy, right?

Conclusion

Always use this trick when you need to download any files in the Browser. Don't complicate yourself with any Server-Side code, or third-party libraries anymore.

Thanks for reading!

Источник: [diseinuak4web.net]
javascript download blob text file

Javascript download blob text file

2 thoughts to “Javascript download blob text file”

Leave a Reply

Your email address will not be published. Required fields are marked *