Created
November 1, 2012 05:02
-
-
Save panzi/3991951 to your computer and use it in GitHub Desktop.
Save/download data generated in JavaScript (2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (BrowserSupportedMimeTypes[mimetype.split(";")[0]] === true) { | |
mimetype = "application/octet-stream"; | |
} | |
blob = builder.getBlob(mimetype); | |
url = URL.createObjectURL(blob); | |
window.open(url, '_blank', ''); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setTimeout(function () { | |
URL.revokeObjectURL(url); | |
}, 250); | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
else if (!/\bMSIE\b/.test(navigator.userAgent)) { | |
showSave = function (data, name, mimetype) { | |
if (!mimetype) mimetype = "application/octet-stream"; | |
// Again I need to filter the mime type so a download is forced. | |
if (BrowserSupportedMimeTypes[mimetype.split(";")[0]] === true) { | |
mimetype = "application/octet-stream"; | |
} | |
window.open("data:"+mimetype+","+encodeURIComponent(data), '_blank', ''); | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function saveData () { | |
if (!showSave) { | |
alert("Your browser does not support any method of saving JavaScript gnerated data to files."); | |
return; | |
} | |
showSave( | |
document.getElementById("data").value, | |
document.getElementById("filename").value, | |
document.getElementById("mimetype").value); | |
} | |
// ]]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
</script> | |
</head> | |
<body> | |
<label for="filename">File name:</label> | |
<input type="text" id="filename" value="Example.txt"/> | |
<label for="mimetype">Mime type:</label> | |
<input type="text" id="mimetype" value="text/plain; charset=UTF-8"/> | |
<label for="data">File contents:</label> | |
<textarea id="data">Multiline text containing unicode characters. | |
German umlauts: äöüÄÖÜ | |
Unicode quotes: »«„“”</textarea> | |
<button onclick="saveData();">Save Data</button> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var blob = new Blob([string],{type:mimetype}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See: http://hackworthy.blogspot.co.at/2012/05/savedownload-data-generated-in.html