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

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

我开始使用 filesaver.js today.I 创建了以下函数:

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

但是当我调用那个函数时我得到"Failed to construct 'Blob': The 1st argument provided is either null, or an invalid Array object. " 有什么想法吗?

因为你不会告诉我们 final_transformation 是什么,我们不得不在没有上下文的情况下进行猜测。试试这个:

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

我遇到了同样的错误。

请参阅 https://developer.mozilla.org/en-US/docs/Web/API/Blob/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.

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

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