Created
November 1, 2012 04:58
-
-
Save panzi/3991938 to your computer and use it in GitHub Desktop.
Save/download data generated in JavaScript (1)
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Save Generated Data</title> | |
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge;chrome=1"/> | |
<script type="text/javascript"> | |
// <![CDATA[ |
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 showSave; | |
var DownloadAttributeSupport = 'download' in document.createElement('a'); |
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 BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder; | |
var URL = window.URL || window.webkitURL || window.mozURL || window.msURL; |
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
navigator.saveBlob = navigator.saveBlob || navigator.msSaveBlob || navigator.mozSaveBlob || navigator.webkitSaveBlob; |
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
window.saveAs = window.saveAs || window.webkitSaveAs || window.mozSaveAs || window.msSaveAs; |
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 BrowserSupportedMimeTypes = { | |
"image/jpeg": true, | |
"image/png": true, | |
"image/gif": true, | |
"image/svg+xml": true, | |
"image/bmp": true, | |
"image/x-windows-bmp": true, | |
"image/webp": true, | |
"audio/wav": true, | |
"audio/mpeg": true, | |
"audio/webm": true, | |
"audio/ogg": true, | |
"video/mpeg": true, | |
"video/webm": true, | |
"video/ogg": true, | |
"text/plain": true, | |
"text/html": true, | |
"text/xml": true, | |
"application/xhtml+xml": true, | |
"application/json": true | |
}; |
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 (BlobBuilder && navigator.saveBlob) { | |
showSave = function (data, name, mimetype) { | |
var builder = new BlobBuilder(); | |
builder.append(data); | |
var blob = builder.getBlob(mimetype||"application/octet-stream"); | |
if (!name) name = "Download.bin"; | |
if (window.saveAs) { | |
window.saveAs(blob, name); | |
} | |
else { | |
navigator.saveBlob(blob, name); | |
} | |
}; | |
} |
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 (BlobBuilder && URL) { | |
showSave = function (data, name, mimetype) { | |
var blob, url, builder = new BlobBuilder(); | |
builder.append(data); | |
if (!mimetype) mimetype = "application/octet-stream"; | |
if (DownloadAttributeSupport) { |
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
blob = builder.getBlob(mimetype); | |
url = URL.createObjectURL(blob); | |
var link = document.createElement("a"); | |
link.setAttribute("href",url); | |
link.setAttribute("download",name||"Download.bin"); |
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 event = document.createEvent('MouseEvents'); | |
event.initMouseEvent('click', true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, null); | |
link.dispatchEvent(event); | |
} | |
else { |
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