Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a dynamic file + link for download in Javascript? [duplicate]

Typically, HTML pages can have link to documents (PDF, etc...) which can be downloaded from the server.

Assuming a Javascript enabled webpage, is it possible to dynamically create a text document (for example) from within the user browser and add a link to download this document without a round trip to the server (or a minimal one)?

In other word, the user would click on a button, the javascript would generate randoms numbers (for example), and put them in a structure. Then, the javascript (JQuery for example) would add a link to the page to download the result as a text file from the structure.

This objective is to keep all (or at least most) of the workload on the user side.

Is this feasible, if yes how?

like image 922
Jérôme Verstrynge Avatar asked Nov 29 '11 12:11

Jérôme Verstrynge


People also ask

How do I download a JavaScript file as a link?

open the Js script link and press ctrl+s i.e save it. Save it by any desired name and then copy it your project folder and then include it in your project files where you have included other files like jquery and css.

How do I trigger a download when clicking HTML button or JavaScript?

To trigger a file download on a button click we will use a custom function or HTML 5 download attribute. The download attribute simply uses an anchor tag to prepare the location of the file that needs to be downloaded.


2 Answers

Here's a solution I've created, that allows you to create and download a file in a single click:

<html> <body>     <button onclick='download_file("my_file.txt", dynamic_text())'>Download</button>     <script>     function dynamic_text() {         return "create your dynamic text here";     }      function download_file(name, contents, mime_type) {         mime_type = mime_type || "text/plain";          var blob = new Blob([contents], {type: mime_type});          var dlink = document.createElement('a');         dlink.download = name;         dlink.href = window.URL.createObjectURL(blob);         dlink.onclick = function(e) {             // revokeObjectURL needs a delay to work properly             var that = this;             setTimeout(function() {                 window.URL.revokeObjectURL(that.href);             }, 1500);         };          dlink.click();         dlink.remove();     }     </script> </body> </html> 

I created this by adapting the code from this HTML5 demo and messing around with things until it worked, so I'm sure there are problems with it (please comment or edit if you have improvements!) but it's a working, single-click solution.

(at least, it works for me on the latest version of Chrome in Windows 7)

like image 152
ahuff44 Avatar answered Oct 03 '22 22:10

ahuff44


By appending a data URI to the page, you can embed a document within the page that can be downloaded. The data portion of the string can be dynamically concatenated using Javascript. You can choose to format it as a URL encoded string or as base64 encoded. When it is base64 encoded, the browser will download the contents as a file. You will have to add a script or jQuery plugin to do the encoding. Here is an example with static data:

jQuery('body').prepend(jQuery('<a/>').attr('href','data:text/octet-stream;base64,SGVsbG8gV29ybGQh').text('Click to download')) 
like image 38
Nate Barr Avatar answered Oct 03 '22 22:10

Nate Barr