Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to construct 'Blob': The 1st argument provided is either null, or an invalid Array object.

I started using filesaver.js today.I have created the following function:

function saving(){
    var blob = new Blob(final_transformation, {type: "text/plain;charset=utf-8"});
saveAs(blob, "hello world.txt");
}

but when i call that function i get"Failed to construct 'Blob': The 1st argument provided is either null, or an invalid Array object. " Any ideas?

like image 920
cssGEEK Avatar asked Feb 24 '15 17:02

cssGEEK


2 Answers

I was getting the same error.

See the Blob constructor documentation at https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob:

var aBlob = new Blob( array[, options]);

array is an Array of ArrayBuffer, ArrayBufferView, Blob, DOMString objects, or a mix of any of such objects, that will be put inside the Blob.

So the first parameter to new Blob is pretty specific - it can only be an array that contains objects of several specific types. A regular string was not working for me, but this works:

> new Blob( [ new TextEncoder().encode( 'some text' ) ], { type: 'text/plain' } )
< Blob {size: 9, type: "text/plain"}
like image 120
We Are All Monica Avatar answered Oct 06 '22 03:10

We Are All Monica


Since you won't tell us what final_transformation is, we have to guess with no context. Try this :

function saving(){
    var blob = new Blob([final_transformation], {type: "text/plain;charset=utf-8"});
saveAs(blob, "hello world.txt");
}
like image 22
AlienWebguy Avatar answered Oct 06 '22 03:10

AlienWebguy