ie8 Save
Description
HTML
<button type="button" onclick="saveTextAsFile();">Save</button>
JavaScript
/**
* Workarounds for browser like ie8 that doesn't supports Blobs...
*/
function SaveContents() {
if (document.execCommand) {
var oWin = window.open("about:blank", "_blank");
//To add a new line, you need to use <br>
oWin.document.writeln("First Line<br>");
oWin.document.writeln("Second Line<br>");
oWin.document.writeln("Third Line");
oWin.document.close();
//Need to specify the filename that we are going to set here
var success = oWin.document.execCommand('SaveAs', true, "output.txt")
oWin.close();
if (!success) {}
}
}
/**
* Most of the Blob related codes are mainly here but if the browser doesn't supports Blob, then you would redirect to the function SaveContents()
*/
function saveTextAsFile() {
//For Blobs, use \r\n for new line
var textToWrite = "First Line\r\n";
textToWrite += "Second Line\r\n";
textToWrite += "Third Line";
//Check if browser supports Blob
if (window.Blob) {
var textFileAsBlob = new Blob([textToWrite], {
type: 'text/plain'
});
//Need to specify the filename that we are going to set here
var fileNameToSaveAs = "output.txt";
var downloadLink = document.createElement("a");
downloadLink.download = fileNameToSaveAs;
downloadLink.innerHTML = "Download File";
if (window.webkitURL != null) {
// Chrome allows the link to be clicked
// without actually adding it to the DOM.
downloadLink.href = window.webkitURL.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);
...