cssGEEK
cssGEEK

Reputation: 1014

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?

Upvotes: 10

Views: 28754

Answers (2)

We Are All Monica
We Are All Monica

Reputation: 13354

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"}

Upvotes: 4

SeanCannon
SeanCannon

Reputation: 78046

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");
}

Upvotes: 26

Related Questions