Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving text in a local file in Internet Explorer 10

Tags:

I need to be able to save a string into a local file. Based on the code in here I got the following going:

function saveTextAsFile(fileNameToSaveAs, textToWrite) { var textFileAsBlob = new Blob([textToWrite], {     type: 'text/plain' });  var downloadLink = document.createElement("a"); downloadLink.download = fileNameToSaveAs; downloadLink.innerHTML = "Download File";  if (true) { //window.webkitURL !== null) {     // Chrome allows the link to be clicked     // without actually adding it to the DOM.     downloadLink.href = window.URL.createObjectURL(textFileAsBlob); } else {     // Firefox requires the link to be added to the DOM     // before it can be clicked.     downloadLink.href = window.URL.createObjectURL(textFileAsBlob);     downloadLink.onclick = destroyClickedElement;     downloadLink.style.display = "none";     document.body.appendChild(downloadLink); }  downloadLink.click(); } 

This works fine for Chrome and Firefox, but not for Internet Explorer 10 as

downloadLink.click(); 

gives:

SCRIPT5: Access is denied. 

Is there any explanation/solution to this ?

thanks!

like image 878
kofifus Avatar asked Sep 12 '13 05:09

kofifus


People also ask

Why do my files open with Internet Explorer?

The issue that File Explorer keeps opening on its own is usually caused by the misbehavior of software on its own. So, in order to fix this problem, you can try restarting File Explorer. Usually, when there is a problem with the program or the application, restarting it is able to fix the problem.


2 Answers

IE 10 and 11 use a distinct syntax for downloading or saving blobs to the client machine. Once you've created a blob, use:

window.navigator.msSaveBlob(blob, 'file.txt');  

or

window.navigator.msSaveOrOpenBlob(blob, 'file.txt'); 

to trigger the file save or file save/open dialog.

For more info, see http://msdn.microsoft.com/en-us/library/ie/hh673542(v=vs.85).aspx

like image 116
mstubna Avatar answered Sep 28 '22 07:09

mstubna


Thx to mstubna, here is a solution for Chrome, FF, and IE>9:

function saveTextAsFile(fileNameToSaveAs, textToWrite) {   /* Saves a text string as a blob file*/     var ie = navigator.userAgent.match(/MSIE\s([\d.]+)/),       ie11 = navigator.userAgent.match(/Trident\/7.0/) && navigator.userAgent.match(/rv:11/),       ieEDGE = navigator.userAgent.match(/Edge/g),       ieVer=(ie ? ie[1] : (ie11 ? 11 : (ieEDGE ? 12 : -1)));    if (ie && ieVer<10) {     console.log("No blobs on IE ver<10");     return;   }    var textFileAsBlob = new Blob([textToWrite], {     type: 'text/plain'   });    if (ieVer>-1) {     window.navigator.msSaveBlob(textFileAsBlob, fileNameToSaveAs);    } else {     var downloadLink = document.createElement("a");     downloadLink.download = fileNameToSaveAs;     downloadLink.href = window.URL.createObjectURL(textFileAsBlob);     downloadLink.onclick = function(e) { document.body.removeChild(e.target); };     downloadLink.style.display = "none";     document.body.appendChild(downloadLink);     downloadLink.click();   } } 
like image 24
kofifus Avatar answered Sep 28 '22 06:09

kofifus