未能构造'Blob':提供的第一个参数为null,或者是无效的Array对象

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

本文关键字:null 对象 或者是 参数 Array 无效 Blob 第一个      更新时间:2024-04-20

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

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

但当我调用该函数时,我得到"构造'Blob'失败:提供的第一个参数要么为null,要么是无效的Array对象。"有什么想法吗?

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

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

我得到了同样的错误。

请参阅Blob构造函数文档,网址为https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob:

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

arrayArrayBufferArrayBufferViewBlobDOMString对象的Array,或者这些对象中的任何对象的组合,它们将被放入Blob中。

因此,new Blob的第一个参数是非常特定的——它只能是一个包含几种特定类型对象的数组。常规字符串对我不起作用,但它起作用:

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