javascript 无法构造“Blob”:提供的第一个参数为 null,或无效的 Array 对象。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28702799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:24:10  来源:igfitidea点击:

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

javascriptjqueryfile-io

提问by cssGEEK

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

我今天开始使用 filesaver.js。我创建了以下函数:

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?

但是当我调用该函数时,我得到“无法构造'Blob':提供的第一个参数为空,或无效的数组对象。”有什么想法吗?

回答by AlienWebguy

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

由于您不会告诉我们是什么final_transformation,我们必须在没有上下文的情况下进行猜测。试试这个 :

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

回答by We Are All Monica

I was getting the same error.

我遇到了同样的错误。

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

请参阅https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob 上的 Blob 构造函数文档:

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

arrayis an Arrayof ArrayBuffer, ArrayBufferView, Blob, DOMStringobjects, or a mix of any of such objects, that will be put inside the Blob.

arrayArrayArrayBufferArrayBufferViewBlobDOMString对象,或上述任何物体的混合,将被内部的放Blob

So the first parameter to new Blobis 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:

所以第一个参数 tonew Blob是非常具体的——它只能是一个包含几种特定类型对象的数组。常规字符串对我不起作用,但这有效:

> new Blob( [ new TextEncoder().encode( 'some text' ) ], { type: 'text/plain' } )
< Blob {size: 9, type: "text/plain"}