var blob = new Blob([byteArray], {type: contentType});
var textFile = null, makeTextFile = function (text) { var data = new Blob([text], {type: 'text/plain'}); // If we are replacing a previously generated file we need to // manually revoke the object URL to avoid memory leaks. if (textFile !== null) { window.URL.revokeObjectURL(textFile); } textFile = window.URL.createObjectURL(data); // returns a URL you can use as a href return textFile; };
var blob = new Blob(["some text"], { type: "text/plain;charset=utf-8;", }); saveAs(blob, "thing.txt");
var data = {a:1, b:2, c:3}; var json = JSON.stringify(data); var blob = new Blob([json], {type: "application/json"}); var url = URL.createObjectURL(blob); var a = document.createElement('a'); a.download = "backup.json"; a.href = url; a.textContent = "Download backup.json";
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")
if (window.navigator.msSaveOrOpenBlob) { var blob = new Blob([decodeURIComponent(encodeURI(result.data))], { type: "text/csv;charset=utf-8;" }); navigator.msSaveBlob(blob, 'FileName.csv'); }
/* TODO: replace the blob content with your byte[] */ var blob = new Blob([yourBinaryDataAsAnArrayOrAsAString], {type: "application/octet-stream"}); var fileName = "myFileName.myExtension"; saveAs(blob, fileName);
var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"}); saveAs(blob, "hello world.txt");
var myBlob = new Blob(); //do stuff here to give the blob some data... var myFile = blobToFile(myBlob, "my-image.png");
var canvas = document.createElement("canvas"); var dataURL = canvas.toDataURL( "image/png" ); var data = atob( dataURL.substring( "data:image/png;base64,".length ) ), asArray = new Uint8Array(data.length); for( var i = 0, len = data.length; i < len; ++i ) { asArray[i] = data.charCodeAt(i); } var blob = new Blob( [ asArray.buffer ], {type: "image/png"} );
var img = document.images[0]; img.onclick = function() { // atob to base64_decode the data-URI var image_data = atob(img.src.split(',')[1]); // Use typed arrays to convert the binary data to a Blob var arraybuffer = new ArrayBuffer(image_data.length); var view = new Uint8Array(arraybuffer); for (var i=0; i<image_data.length; i++) { view[i] = image_data.charCodeAt(i) & 0xff; } try { // This is the recommended method: var blob = new Blob([arraybuffer], {type: 'application/octet-stream'}); } catch (e) { // The BlobBuilder API has been deprecated in favour of Blob, but older // browsers don't know about the Blob constructor // IE10 also supports BlobBuilder, but since the `Blob` constructor // also works, there's no need to add `MSBlobBuilder`. var bb = new (window.WebKitBlobBuilder || window.MozBlobBuilder); bb.append(arraybuffer); var blob = bb.getBlob('application/octet-stream'); // <-- Here's the Blob } // Use the URL object to create a temporary URL var url = (window.webkitURL || window.URL).createObjectURL(blob); location.href = url; // <-- Download! };
var xhr = new XMLHttpRequest(); xhr.open("GET", requestUrl); xhr.responseType = "arraybuffer"; xhr.onload = function () { if (this.status === 200) { var blob = new Blob([xhr.response], {type: "application/pdf"}); var objectUrl = URL.createObjectURL(blob); window.open(objectUrl); } }; xhr.send();
var array = new Int8Array([17, -45.3]); try{ var jpeg = new Blob( [array], {type : "image/jpeg"}); } catch(e){ // TypeError old chrome and FF window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder; if(e.name == 'TypeError' && window.BlobBuilder){ var bb = new BlobBuilder(); bb.append(array.buffer); var jpeg = bb.getBlob("image/jpeg"); } else if(e.name == "InvalidStateError"){ // InvalidStateError (tested on FF13 WinXP) var jpeg = new Blob( [array.buffer], {type : "image/jpeg"}); } else{ // We're screwed, blob constructor unsupported entirely } }
var code = "while(true){}"; var URL = window.webkitURL || window.URL; var bb = new Blob([code], {type : 'text/javascript'}); code = URL.createObjectURL(bb); new Worker(code); new Worker(code); new Worker(code); new Worker(code);
var textFile = null, makeTextFile = function (text) { var data = new Blob([text], {type: 'text/plain'}); // If we are replacing a previously generated file we need to // manually revoke the object URL to avoid memory leaks. if (textFile !== null) { window.URL.revokeObjectURL(textFile); } textFile = window.URL.createObjectURL(data); // returns a URL you can use as a href return textFile; };
if ( window.navigator.msSaveOrOpenBlob && window.Blob ) { var blob = new Blob( [ "A,B\nC,D" ], { type: "text/csv" } ); navigator.msSaveOrOpenBlob( blob, "strings.csv" ); }
var BuildWorker = function(foo){ var str = foo.toString() .match(/^\s*function\s*\(\s*\)\s*\{(([\s\S](?!\}$))*[\s\S])/)[1]; return new Worker(window.URL.createObjectURL( new Blob([str],{type:'text/javascript'}))); }
if(!HTMLCanvasElement.prototype.toBlob){ HTMLCanvasElement.prototype.toBlob = function(callback, type, encoderOptions){ var dataurl = this.toDataURL(type, encoderOptions); var bstr = atob(dataurl.split(',')[1]), n = bstr.length, u8arr = new Uint8Array(n); while(n--){ u8arr[n] = bstr.charCodeAt(n); } var blob = new Blob([u8arr], {type: type}); callback.call(this, blob); }; }
$.ajax({ url: apiRoot + "/proxy", data: {url: "http://example.com/image.jpg"}, success: function(data) { var binary = atob(data.split(',')[1]); var array = []; for (var i = 0; i < binary.length; i++) { array.push(binary.charCodeAt(i)); } var file = new Blob([new Uint8Array(array)], {type: 'image/jpeg'}); } });
var xhr = new XMLHttpRequest(); xhr.responseType = 'arraybuffer'; xhr.onload = function() { var blb = new Blob([xhr.response], {type: 'image/png'}); var url = (window.URL || window.webkitURL).createObjectURL(blb); image.src = url; } xhr.open('GET', 'http://whatever.com/wherever'); xhr.send();