How to create a file in memory for user to download, but not through server?
Asked Answered
T

22

1126

Is there a way to create a text file on the client side and prompt the user to download it without any interaction with the server?

I know I can't write directly to their machine (security and all), but can I create the file and prompt them to save it?

Tombolo answered 8/9, 2010 at 6:24 Comment(2)
As of April 2014, FileSytem APIs may not be standardized in W3C. Anyone looking at the solution with blob should thread with caution, I guess. HTML5 rocks heads up W3C Mailing List on FileSytem APIUnwinking
See also: JavaScript: Create and save fileProem
H
488

You can use data URIs. Browser support varies; see Wikipedia. Example:

<a href="data:application/octet-stream;charset=utf-16le;base64,//5mAG8AbwAgAGIAYQByAAoA">text file</a>

The octet-stream is to force a download prompt. Otherwise, it will probably open in the browser.

For CSV, you can use:

<a href="data:application/octet-stream,field1%2Cfield2%0Afoo%2Cbar%0Agoo%2Cgai%0A">CSV Octet</a>

Try the jsFiddle demo.

Handicapper answered 8/9, 2010 at 6:29 Comment(26)
This is not a cross browser solution but definitely something worth looking at. For example IE limits support to data uri. IE 8 limits size to 32KB and IE 7 and lower doesn't support at all.Hah
@Darin, yes, this won't work in any version of IE, because data URIs are not allowed in a elements.Handicapper
in Chrome Version 19.0.1084.46, this method generates the following warning : "Resource interpreted as Document but transferred with MIME type text/csv: "data:text/csv,field1%2Cfield2%0Afoo%2Cbar%0Agoo%2Cgai%0A"." A download is not triggeredChair
@Chris, yes, apparently it doesn't work in Chrome, even with octet-stream.Handicapper
It does work in Chrome now (tested against v20 and v21) but not IE9 (that might just be the jsFiddle, but somehow I doubt it).Surmount
I'm accepting this answer, since there seems to be no other way to accomplish this...Tombolo
Does not work in ie8 and ie9 because these browsers only support data uri for images. Ref: #12043347Dunsinane
As naren noted with his answer, Firefox needs the link to be present in the document in order for the download to occur.Trapezoid
Doesn't work at all in Safari (7.0.4). Also, as of Chrome 36.0.1985.97 beta it doesn't seem possible to name the file using the download attribute.Riccio
Also, using Chrome 37.0.2062.120, the browser crashes when the base64 string gets too long.Compartmentalize
I was like - who is that linking to wikipedia for browser compat - oh ok :)Mcmillian
The correct charset is almost certainly UTF-16, unless you have code converting it to UTF-8. JavaScript uses UTF-16 internally. If you have a text or CSV file, start the string with '\ufeff', the Byte Order Mark for UTF-16BE, and text editors will be able to read non-ASCII characters correctly.Blowfly
@larspars, good point. It is possible to make a UTF-8 data URL (and my example was one), but probably easier to make a UTF-16 one. Technically, though, JavaScript is closer to UCS-2.Handicapper
Does not data URIs have size limitations?Unbated
@Avinav, yes, at least in certain browsers (e.g. IE 8).Handicapper
if there a way to give an understandable name to an image whose src is a data URI?Elle
Does not work in Samsung Browser (Default mobile browser)Heddi
Just add download="txt.csv" attribute in order to have proper file name and extension and to tell your OS what to do with it.Iou
And there is a size limit. It is said that for Chrome, the limit is 2MB.Ramses
Works like a charm on Chrome. base64 seems to be the best solution to pass PDF files :)Betancourt
Using <a> attribute download="filename.txt" and javascript .click() you can automate the process.Entrap
It does not work in Microsoft Edge 40.15063.674.0 and Internet Explorer 11.674.15063.0. I used the answer from @naren and it works.Florindaflorine
I'm trying to use this to download the value of a textarea as a text file but the newlines in the textarea are not shown in the downloaded text file. Suggestions? Here is how I form the href: 'data:application/octet-stream,' + encodeURIComponent(document.getElementById("editor").value).replace("\n","%0A") . (I added the replace function hoping that might be the issue).Dodi
No longer works in Firefox; top level data URIs have been blocked for "security". bugzilla.mozilla.org/show_bug.cgi?id=1331351Firestone
It work great in Firefox for me, but note that DataURI's are subject to size limitations, which vary by browser maybe 32kb, maybe 100kb, maybe 4gb (good luck finding a straight answer on actual limits!)Pinnace
This is not a viable solution. I would rather create a Blob with the proper mime type, then add it to an anchor tag which I will trigger a click on. The advantage of this solution is that you don't have to explicitly create an ObjectUrl.Constitutionality
P
1023

Simple solution for HTML5 ready browsers...

function download(filename, text) {
  var element = document.createElement('a');
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  element.setAttribute('download', filename);

  element.style.display = 'none';
  document.body.appendChild(element);

  element.click();

  document.body.removeChild(element);
}
form * {
  display: block;
  margin: 10px;
}
<form onsubmit="download(this['name'].value, this['text'].value)">
  <input type="text" name="name" value="test.txt">
  <textarea name="text"></textarea>
  <input type="submit" value="Download">
</form>

Usage

download('test.txt', 'Hello world!');
Parkway answered 12/8, 2013 at 21:57 Comment(21)
Yep. This is exactly what @MatthewFlaschen has posted here about 3 years ago.Tombolo
Yes, but with download attribute you can specify file name ;-)Topknot
As @earcam has already pointed out in the comments above.Tombolo
Chrome only appends the txt extension if you do not provide an extension in the filename. If you do download("data.json", data) it'll work as expected.Hunsaker
In chrome if I use your code outside a submit form, and named my file *.js for example, chrome tell me its a malware. Any idea how to fix this ?Epileptoid
@Epileptoid Same situation with this fiddle? Maybe this is related: Malware and unwanted softwareTopknot
This will remove any newline in the downloaded text. My file only contains 1 line. Any Idea on how to fix that? If I do data:text/plain;base64 firefox won't download it.Promissory
@Promissory You are opening that file in Windows Notepad. Am I right? Try Sublime Text, VSCode, Atom, Notepad++, etc. Notepad expecting, that lines ends with sequence \n\r, but this method saves new lines as \n only.Topknot
@MatějPokorný I am using Notepad++. I have also tried JSON.stringify(item, undefined, "\t").replace(/\n/g, "\r\n");. When I print the text on the console, immediately before inserting it into the data uri, I can see that it contains \r\n, however, when I open the saved file those are gone. I think I better open an own question.Promissory
I opened an own question here: #49128696Promissory
This worked for me in Chrome (73.0.3683.86), and Firefox (66.0.2). It did NOT work in IE11 (11.379.17763.0) and Edge (44.17763.1.0).Coolant
How can I do this with a jpeg or png file?Rambort
Does it work for Mobile App or Mobile browser? I have tried this for iPhone Safari browser, but It does not download the file in download folderFlorinda
There's no need to attach the element to the DOM.Birchard
@Birchard is right, remove document.body.appendChild(element); and document.body.removeChild(element);, works just as well.Electret
if i have to send formData in this, how will i send thatLansing
works for me using Chrome, but oddly, all UI elements are empty at the end. I wonder why.Chose
This method will cause problems with very large files. Look at the other answers that use URL.createObjectURL for more performant and stable solution.Mornay
will this also work with other file types f.e. mp3? would the data attribute be "audio/mp3" then?Crapulous
I wonder if you can do this with rtf, pdf as well (just in plain javascript)Upholster
This no longer works under Chrome. Apparently for security reasons. Error message is "Uncaught DOMException: Failed to read the 'localStorage' property from 'Window': The document is sandboxed and lacks the 'allow-same-origin' flag."Kopje
H
488

You can use data URIs. Browser support varies; see Wikipedia. Example:

<a href="data:application/octet-stream;charset=utf-16le;base64,//5mAG8AbwAgAGIAYQByAAoA">text file</a>

The octet-stream is to force a download prompt. Otherwise, it will probably open in the browser.

For CSV, you can use:

<a href="data:application/octet-stream,field1%2Cfield2%0Afoo%2Cbar%0Agoo%2Cgai%0A">CSV Octet</a>

Try the jsFiddle demo.

Handicapper answered 8/9, 2010 at 6:29 Comment(26)
This is not a cross browser solution but definitely something worth looking at. For example IE limits support to data uri. IE 8 limits size to 32KB and IE 7 and lower doesn't support at all.Hah
@Darin, yes, this won't work in any version of IE, because data URIs are not allowed in a elements.Handicapper
in Chrome Version 19.0.1084.46, this method generates the following warning : "Resource interpreted as Document but transferred with MIME type text/csv: "data:text/csv,field1%2Cfield2%0Afoo%2Cbar%0Agoo%2Cgai%0A"." A download is not triggeredChair
@Chris, yes, apparently it doesn't work in Chrome, even with octet-stream.Handicapper
It does work in Chrome now (tested against v20 and v21) but not IE9 (that might just be the jsFiddle, but somehow I doubt it).Surmount
I'm accepting this answer, since there seems to be no other way to accomplish this...Tombolo
Does not work in ie8 and ie9 because these browsers only support data uri for images. Ref: #12043347Dunsinane
As naren noted with his answer, Firefox needs the link to be present in the document in order for the download to occur.Trapezoid
Doesn't work at all in Safari (7.0.4). Also, as of Chrome 36.0.1985.97 beta it doesn't seem possible to name the file using the download attribute.Riccio
Also, using Chrome 37.0.2062.120, the browser crashes when the base64 string gets too long.Compartmentalize
I was like - who is that linking to wikipedia for browser compat - oh ok :)Mcmillian
The correct charset is almost certainly UTF-16, unless you have code converting it to UTF-8. JavaScript uses UTF-16 internally. If you have a text or CSV file, start the string with '\ufeff', the Byte Order Mark for UTF-16BE, and text editors will be able to read non-ASCII characters correctly.Blowfly
@larspars, good point. It is possible to make a UTF-8 data URL (and my example was one), but probably easier to make a UTF-16 one. Technically, though, JavaScript is closer to UCS-2.Handicapper
Does not data URIs have size limitations?Unbated
@Avinav, yes, at least in certain browsers (e.g. IE 8).Handicapper
if there a way to give an understandable name to an image whose src is a data URI?Elle
Does not work in Samsung Browser (Default mobile browser)Heddi
Just add download="txt.csv" attribute in order to have proper file name and extension and to tell your OS what to do with it.Iou
And there is a size limit. It is said that for Chrome, the limit is 2MB.Ramses
Works like a charm on Chrome. base64 seems to be the best solution to pass PDF files :)Betancourt
Using <a> attribute download="filename.txt" and javascript .click() you can automate the process.Entrap
It does not work in Microsoft Edge 40.15063.674.0 and Internet Explorer 11.674.15063.0. I used the answer from @naren and it works.Florindaflorine
I'm trying to use this to download the value of a textarea as a text file but the newlines in the textarea are not shown in the downloaded text file. Suggestions? Here is how I form the href: 'data:application/octet-stream,' + encodeURIComponent(document.getElementById("editor").value).replace("\n","%0A") . (I added the replace function hoping that might be the issue).Dodi
No longer works in Firefox; top level data URIs have been blocked for "security". bugzilla.mozilla.org/show_bug.cgi?id=1331351Firestone
It work great in Firefox for me, but note that DataURI's are subject to size limitations, which vary by browser maybe 32kb, maybe 100kb, maybe 4gb (good luck finding a straight answer on actual limits!)Pinnace
This is not a viable solution. I would rather create a Blob with the proper mime type, then add it to an anchor tag which I will trigger a click on. The advantage of this solution is that you don't have to explicitly create an ObjectUrl.Constitutionality
T
327

An example for IE 10+, Firefox and Chrome (and without jQuery or any other library):

function save(filename, data) {
    const blob = new Blob([data], {type: 'text/csv'});
    if(window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveBlob(blob, filename);
    }
    else{
        const elem = window.document.createElement('a');
        elem.href = window.URL.createObjectURL(blob);
        elem.download = filename;        
        document.body.appendChild(elem);
        elem.click();        
        document.body.removeChild(elem);
    }
}

Note that, depending on your situation, you may also want to call URL.revokeObjectURL after removing elem. According to the docs for URL.createObjectURL:

Each time you call createObjectURL(), a new object URL is created, even if you've already created one for the same object. Each of these must be released by calling URL.revokeObjectURL() when you no longer need them. Browsers will release these automatically when the document is unloaded; however, for optimal performance and memory usage, if there are safe times when you can explicitly unload them, you should do so.

Topi answered 5/11, 2015 at 10:42 Comment(14)
In Chrome, I didn't actually have to append the element to the body to get this to work.Labana
There is a size limit. For Chrome, it is said to be 2MB. Anyone has idea on how to deal with larger file?Ramses
Calling window.URL.revokeObjectURL(elem.href); after removing elem on Firefox 48 results in the download window never appearing and the download never starting. Removing the call to revokeObjectURL() solves this. For AngularJS 1.x apps, I guess that you can build an array of Urls that are cleaned up on destroy of the controller.Brittaneybrittani
For AngularJS 1.x apps, you can build an array of Urls as they are created and then clean them up in the $onDestroy function of the component. This is working great for me.Brittaneybrittani
This combines naren's solution, with Dzarek's handling of IE.Equerry
Other answers led to Failed: network error in Chrome. This one works well.Bandanna
"All the above solutions didn't work in all browsers". Since the order of answers can change over time, it's unclear which answers were above yours when you wrote this. Can you indicate exactly which approaches don't work in all browsers?Sports
This worked for me in Chrome (73.0.3683.86), Firefox (66.0.2), IE11 (11.379.17763.0) and Edge (44.17763.1.0).Coolant
Kanchu's solution on the duplicate of this question includes some small improvements.Ablaze
that bypasses the URI size limit.Aloha
For those looking to avoid garbage collection on the URL or strange behavior, just declare your blob like this: const url = URL.createObjectURL(blob, { oneTimeOnly: true }). You can always save the blob and generate a new Url later if needed.Oldenburg
Consider adding elem.style.display = 'none'; before document.body.appendChild(elem); if you want to avoid any potential for visual glitchesTalie
msSaveOrOpenBlob seems to be deprecated. So I think you shoud edit this answer. developer.mozilla.org/en-US/docs/Web/API/Navigator/…Atmosphere
@Atmosphere You are right, it's compatible with older version of Internet Explorer. But that's for this reason that it is in this answer, to be compatible with many browser. If the function doesn't exist in the browser createObjectURL will be called instead.Topi
V
204

All of the above example works just fine in chrome and IE, but fail in Firefox. Please do consider appending an anchor to the body and removing it after click.

var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob(['Test,Text'], {type: 'text/csv'}));
a.download = 'test.csv';

// Append anchor to body.
document.body.appendChild(a);
a.click();

// Remove anchor from body
document.body.removeChild(a);
Vins answered 25/11, 2013 at 13:37 Comment(4)
However: there's an open bug in IE 10 (and I've still seen it in 11) that throws "Access is denied" on the a.click() line because it thinks the blob URL is cross-origin.Schmit
@Schmit data uri is cross origin in some browsers. as far as I know, not just in msie, but in chrome as well. you can test it by trying to inject javascript with data uri. It won't be able to access other parts of the site...Reyna
"All of the above example works just fine in chrome and IE, but fail in Firefox.". Since the order of answers can change over time, it's unclear which answers were above yours when you wrote this. Can you indicate exactly which approaches don't work in Firefox?Sports
👍 This blob approach works much better for very large files.Langevin
B
136

I'm happily using FileSaver.js. Its compatibility is pretty good (IE10+ and everything else), and it's very simple to use:

var blob = new Blob(["some text"], {
    type: "text/plain;charset=utf-8;",
});
saveAs(blob, "thing.txt");
Barbaresi answered 8/5, 2013 at 23:5 Comment(9)
This works great on Chrome. How do I allow the user to specific the location of the file on disk?Emmaemmalee
Wow, thanks for the easy to use library. This is easily the best answer, and who cares about people using HTML < 5 these days any ways?Breena
@Emmaemmalee I'm not sure you can with this plugin.Barbaresi
@gregm: You mean the download location? That's not related to FileSaver.js, you need to set your browser configuration so that it asks for a folder before every download, or use the rather new download attribute on <a>.Variate
This is a GREAT solution for IE 10+ family of browsers. IE doesn't support the download HTML 5 tag yet and the other solutions on this page (and other SO pages discussing the same problem) were simply not working for me. FileSaver ftw!Sym
It does not work in Samsung Browser 3 (Default browser on at least Galaxy 6)Heddi
@CoDEmanX how do you set the attribute? All I see is a function call...Trivial
I was referring to <a download="...">. I'm not sure if/how FileSaver.js supports this.Variate
It looks like demo reloads page. History in dev tools is updated. Binary octect looks better as solution.Youngyoungblood
I
48

Use Blob:

function download(content, mimeType, filename){
  const a = document.createElement('a') // Create "a" element
  const blob = new Blob([content], {type: mimeType}) // Create a blob (file-like object)
  const url = URL.createObjectURL(blob) // Create an object URL from blob
  a.setAttribute('href', url) // Set "a" element link
  a.setAttribute('download', filename) // Set download filename
  a.click() // Start downloading
}

Blob is being supported by all modern browsers.
Caniuse support table for Blob:

Here is a Fiddle

And here MDN Docs

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...

Ill answered 19/11, 2020 at 8:57 Comment(5)
This is a copy of the existing answer here: https://mcmap.net/q/42081/-how-to-create-a-file-in-memory-for-user-to-download-but-not-through-serverOldenburg
@Oldenburg This is not a copy, there's subtle but important difference between the two answers.Hindrance
How so @xuhdev? This answer contains the exact same elements as the other answer but without the additional window.navigator.msSaveOrOpenBlob alternative. That doesn't make this a new answer. The other answer uses Blob + using URL.createObjectURL + a dynamically created <a> tag whose click is immediately called.Oldenburg
@Oldenburg This answer does not modify document as the other one does. The other answer doesn't explain why modifying the DOM is needed and what side effect it may have. So I think there are some positive points on this answer (hence "important difference between the two answers"). Of course, the other answer has its own positive sides, as you pointed out.Hindrance
Thanks for the clarification @xuhdev, fair enough 👍 I've retracted my flag but maintain the downvote because I think the difference about the DOM could have been a comment or edit to the other answer I mentioned.Oldenburg
V
22

The following method works in IE11+, Firefox 25+ and Chrome 30+:

<a id="export" class="myButton" download="" href="#">export</a>
<script>
    function createDownloadLink(anchorSelector, str, fileName){
        if(window.navigator.msSaveOrOpenBlob) {
            var fileData = [str];
            blobObject = new Blob(fileData);
            $(anchorSelector).click(function(){
                window.navigator.msSaveOrOpenBlob(blobObject, fileName);
            });
        } else {
            var url = "data:text/plain;charset=utf-8," + encodeURIComponent(str);
            $(anchorSelector).attr("download", fileName);               
            $(anchorSelector).attr("href", url);
        }
    }

    $(function () {
        var str = "hi,file";
        createDownloadLink("#export",str,"file.txt");
    });

</script>

See this in Action: http://jsfiddle.net/Kg7eA/

Firefox and Chrome support data URI for navigation, which allows us to create files by navigating to a data URI, while IE doesn't support it for security purposes.

On the other hand, IE has API for saving a blob, which can be used to create and download files.

Veneration answered 27/12, 2013 at 6:55 Comment(3)
I just used jquery to attach events(onclick and onready) and set attributes, which you can also do with vanilla JS. The core part(window.navigator.msSaveOrOpenBlob) doesn't need jquery.Veneration
There is still the limitation of size for the data uri approach, isn't it?Plante
msSaveOrOpenBlob is shown as obsolete here: developer.mozilla.org/en-US/docs/Web/API/Navigator/msSaveBlobGovan
D
22

We can use the URL api, in particular URL.createObjectURL(), and the Blob api to encode and download pretty much anything.

If your download is small, this works fine:

document.body.innerHTML += 
`<a id="download" download="PATTERN.json" href="${URL.createObjectURL(new Blob([JSON.stringify("HELLO WORLD", null, 2)]))}"> Click me</a>`
download.click()
download.outerHTML = ""

If your download is huge, instead of using the DOM, a better way is to create a link element with the download parameters, and trigger a click.

Notice the link element isn't appended to the document but the click work anyway! This is possible to create a download of many hundreds of Mo this way, as the DOM is not modified (Otherwise the huge URL in the DOM can be a source of tab freeze).

const stack = {
 some: "stuffs",
 alot: "of them!"
}

BUTTONDOWNLOAD.onclick = (function(){
  let j = document.createElement("a")
  j.download = "stack_"+Date.now()+".json"
  j.href = URL.createObjectURL(new Blob([JSON.stringify(stack, null, 2)]))
  j.click()
})
<button id="BUTTONDOWNLOAD">DOWNLOAD!</button>

Bonus! Download any cyclic objects, avoid the errors:

TypeError: cyclic object value (Firefox) TypeError: Converting

circular structure to JSON (Chrome and Opera) TypeError: Circular

reference in value argument not supported (Edge)

Using https://github.com/douglascrockford/JSON-js/blob/master/cycle.js

On this example, downloading the document object as json.

/* JSON.decycle */
if(typeof JSON.decycle!=="function"){JSON.decycle=function decycle(object,replacer){"use strict";var objects=new WeakMap();return(function derez(value,path){var old_path;var nu;if(replacer!==undefined){value=replacer(value)}
if(typeof value==="object"&&value!==null&&!(value instanceof Boolean)&&!(value instanceof Date)&&!(value instanceof Number)&&!(value instanceof RegExp)&&!(value instanceof String)){old_path=objects.get(value);if(old_path!==undefined){return{$ref:old_path}}
objects.set(value,path);if(Array.isArray(value)){nu=[];value.forEach(function(element,i){nu[i]=derez(element,path+"["+i+"]")})}else{nu={};Object.keys(value).forEach(function(name){nu[name]=derez(value[name],path+"["+JSON.stringify(name)+"]")})}
return nu}
return value}(object,"$"))}}


document.body.innerHTML += 
`<a id="download" download="PATTERN.json" href="${URL.createObjectURL(new Blob([JSON.stringify(JSON.decycle(document), null, 2)]))}"></a>`
download.click()
Dreadful answered 31/7, 2019 at 22:54 Comment(1)
The best and easy solution to use! Thank youHodgkins
H
18

The package js-file-download from github.com/kennethjiang/js-file-download handles edge cases for browser support:

View source to see how it uses techniques mentioned on this page.

Installation

yarn add js-file-download
npm install --save js-file-download

Usage

import fileDownload from 'js-file-download'

// fileDownload(data, filename, mime)
// mime is optional

fileDownload(data, 'filename.csv', 'text/csv')
Hammad answered 4/2, 2019 at 23:17 Comment(1)
Thanks - just tested - works with Firefox, Chrome and Edge on WindowsCounteraccusation
S
16

This solution is extracted directly from tiddlywiki's (tiddlywiki.com) github repository. I have used tiddlywiki in almost all browsers and it works like a charm:

function(filename,text){
    // Set up the link
    var link = document.createElement("a");
    link.setAttribute("target","_blank");
    if(Blob !== undefined) {
        var blob = new Blob([text], {type: "text/plain"});
        link.setAttribute("href", URL.createObjectURL(blob));
    } else {
        link.setAttribute("href","data:text/plain," + encodeURIComponent(text));
    }
    link.setAttribute("download",filename);
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}

Github repo: Download saver module

Sordino answered 15/7, 2015 at 18:52 Comment(2)
It works very nicely on Chrome, but not on Firefox. It does make a file and downloads it, but the file is empty. No content. Any ideas why? Haven't tested on IE...Lecture
except that the function has no name, this is my favouriteJungle
Y
15
function download(filename, text) {
  var element = document.createElement('a');
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  element.setAttribute('download', filename);

  element.style.display = 'none';
  document.body.appendChild(element);

  element.click();

  document.body.removeChild(element);
}



// Start file download.
download("hello.txt","This is the content of my file :)");

Original article : https://ourcodeworld.com/articles/read/189/how-to-create-a-file-and-generate-a-download-with-javascript-in-the-browser-without-a-server

Yorgen answered 23/3, 2021 at 11:3 Comment(0)
S
12

If you just want to convert a string to be available for download you can try this using jQuery.

$('a.download').attr('href', 'data:application/csv;charset=utf-8,' + encodeURI(data));
Spica answered 2/2, 2015 at 1:25 Comment(1)
Scape data with encodeURI might be needed as I suggested here before being able to comment: https://mcmap.net/q/42081/-how-to-create-a-file-in-memory-for-user-to-download-but-not-through-serverSelway
D
11

Solution that work on IE10: (I needed a csv file, but it's enough to change type and filename to txt)

var csvContent=data; //here we load our csv data 
var blob = new Blob([csvContent],{
    type: "text/csv;charset=utf-8;"
});

navigator.msSaveBlob(blob, "filename.csv")
Domineca answered 8/11, 2013 at 10:32 Comment(1)
Ludovic's answer includes this big, plus support for the other browsers.Equerry
C
9

As mentioned before, filesaver is a great package to work with files on the client side. But, it is not do well with large files. StreamSaver.js is an alternative solution (which is pointed in FileServer.js) that can handle large files:

const fileStream = streamSaver.createWriteStream('filename.txt', size);
const writer = fileStream.getWriter();
for(var i = 0; i < 100; i++){
    var uint8array = new TextEncoder("utf-8").encode("Plain Text");
    writer.write(uint8array);
}
writer.close()
Cornu answered 17/5, 2017 at 10:55 Comment(1)
Text Encoder is highly experimental right now, I'd suggest avoiding (or polyfilling) it.Telephotography
A
8
var element = document.createElement('a');
element.setAttribute('href', 'data:text/text;charset=utf-8,' +      encodeURI(data));
element.setAttribute('download', "fileName.txt");
element.click();
Ardie answered 9/5, 2015 at 11:36 Comment(1)
What are the differences between this approach and creating a Blob?Equerry
S
7

Based on @Rick answer which was really helpful.

You have to scape the string data if you want to share it this way:

$('a.download').attr('href', 'data:application/csv;charset=utf-8,'+ encodeURI(data));

` Sorry I can not comment on @Rick's answer due to my current low reputation in StackOverflow.

An edit suggestion was shared and rejected.

Selway answered 7/9, 2015 at 14:59 Comment(1)
I was not able to accept the suggestion. Strange... I updated the code.Spica
D
3

This below function worked.

 private createDownloadableCsvFile(fileName, content) {
   let link = document.createElement("a");
   link.download = fileName;
   link.href = `data:application/octet-stream,${content}`;
   return link;
 }
Debroahdebs answered 25/2, 2019 at 2:29 Comment(1)
can you open the file in a new tab keeping the fileName assigned, but not downloading, just opening in a tab?Prowler
H
2

Download file with extensions or without extensions in the example, I am using JSON. You may add your data and extensions. You may use 'MAC-Addresses.json' here, as per your wish. If you want to add an extension, add there, else, just write the file name without extensions.

let myJson = JSON.stringify(yourdata);
    let element = document.createElement('a');
    element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(myJson));
    element.setAttribute('download', 'MAC-Addresses.json');
    element.style.display = 'none';
    document.body.appendChild(element);
    element.click();
    document.body.removeChild(element);
Hornet answered 15/10, 2021 at 7:58 Comment(1)
Nice code snippet, was useful for me.Ducharme
C
1

For me this worked perfectly, with the same filename and extension getting downloaded

<a href={"data:application/octet-stream;charset=utf-16le;base64," + file64 } download={title} >{title}</a>

'title' is the file name with extension i.e, sample.pdf, waterfall.jpg, etc..

'file64' is the base64 content something like this i.e, Ww6IDEwNDAsIFNsaWRpbmdTY2FsZUdyb3VwOiAiR3JvdXAgQiIsIE1lZGljYWxWaXNpdEZsYXRGZWU6IDM1LCBEZW50YWxQYXltZW50UGVyY2VudGFnZTogMjUsIFByb2NlZHVyZVBlcmNlbnQ6IDcwLKCFfSB7IkdyYW5kVG90YWwiOjEwNDAsIlNsaWRpbmdTY2FsZUdyb3VwIjoiR3JvdXAgQiIsIk1lZGljYWxWaXNpdEZsYXRGZWUiOjM1LCJEZW50YWxQYXltZW50UGVyY2VudGFnZSI6MjUsIlByb2NlZHVyZVBlcmNlbnQiOjcwLCJDcmVhdGVkX0J5IjoiVGVycnkgTGVlIiwiUGF0aWVudExpc3QiOlt7IlBhdGllbnRO

Coonan answered 17/10, 2018 at 10:13 Comment(0)
C
1

I would use an <a></a> tag then set the href='path'. Afterwards, place an image in between the <a> elements so that I can have a visual to see it. If you wanted to, you could create a function that will change the href so that it won't just be the same link but be dynamic.

Give the <a> tag an id as well if you want to access it with javascript.

Starting with the HTML Version:

<a href="mp3/tupac_shakur-how-do-you-want-it.mp3" download id="mp3Anchor">
     <img src="some image that you want" alt="some description" width="100px" height="100px" />
</a>

Now with JavaScript:

*Create a small json file*;

const array = [
     "mp3/tupac_shakur-how-do-you-want-it.mp3",
     "mp3/spice_one-born-to-die.mp3",
     "mp3/captain_planet_theme_song.mp3",
     "mp3/tenchu-intro.mp3",
     "mp3/resident_evil_nemesis-intro-theme.mp3"
];

//load this function on window
window.addEventListener("load", downloadList);

//now create a function that will change the content of the href with every click
function downloadList() {
     var changeHref=document.getElementById("mp3Anchor");

     var j = -1;

     changeHref.addEventListener("click", ()=> {

           if(j < array.length-1) {
               j +=1;
               changeHref.href=""+array[j];
          }
           else {
               alert("No more content to download");
          }
}
Crenelation answered 14/11, 2019 at 19:9 Comment(0)
G
1

The following method works in IE10+, Edge, Opera, FF and Chrome:

const saveDownloadedData = (fileName, data) => {
    if(~navigator.userAgent.indexOf('MSIE') || ~navigator.appVersion.indexOf('Trident/')) { /* IE9-11 */
        const blob = new Blob([data], { type: 'text/csv;charset=utf-8;' });
        navigator.msSaveBlob(blob, fileName);
    } else {
        const link = document.createElement('a')
        link.setAttribute('target', '_blank');
        if(Blob !== undefined) {
            const blob = new Blob([data], { type: 'text/plain' });
            link.setAttribute('href', URL.createObjectURL(blob));
        } else {
            link.setAttribute('href', 'data:text/plain,' + encodeURIComponent(data));
        }

        ~window.navigator.userAgent.indexOf('Edge')
            && (fileName = fileName.replace(/[&\/\\#,+$~%.'':*?<>{}]/g, '_')); /* Edge */

        link.setAttribute('download', fileName);
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    }
}

So, just call the function:

saveDownloadedData('test.txt', 'Lorem ipsum');
Gillian answered 14/3, 2020 at 2:17 Comment(0)
G
-27

If the file contains text data, a technique I use is to put the text into a textarea element and have the user select it (click in textarea then ctrl-A) then copy followed by a paste to a text editor.

Grassy answered 8/9, 2010 at 6:51 Comment(1)
I had considered that, but from a user-friendliness point, this is disastrous. Also, the file has to be saved with a CSV extension. Try telling that to your users.Tombolo

© 2022 - 2024 — McMap. All rights reserved.