Javascript 无法构造“文件”:当使用 JSON.stringify() 时,迭代器 getter 在 chrome 60 中不可调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45512546/
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
Failed to construct 'File': Iterator getter is not callable in chrome 60 when use JSON.stringify()
提问by diEcho
System Configuration : macOS Sierra 10.12.5 ; chrome 60;
系统配置:macOS Sierra 10.12.5;铬 60;
I am trying to download JSON data ( response object ) as a json file but when I tried to achive this using browser File() object , it gives error
我正在尝试将 JSON 数据(响应对象)下载为 json 文件,但是当我尝试使用浏览器 File() 对象实现此目的时,它给出了错误
Failed to construct 'File': Iterator getter is not callable.
Failed to construct 'File': Iterator getter is not callable.
below is my code
下面是我的代码
//`inputData` is the valid response getting from $http.get
var stringifyData = JSON.stringify(inputData);
console.log("stringifyData ", stringifyData);
var file = new File(blob, "filename.json", {type: "text/json;charset=utf-8"});
console.log("file", file);
what is the issue and why I am getting this error. when I use JSON.stringify(inputData, undefined, 4);then it gives no error and displays proper object in the console.
有什么问题以及为什么我会收到此错误。当我使用JSON.stringify(inputData, undefined, 4);then 它不会出错并在控制台中显示正确的对象。
回答by Pavlo
In your case Fileconstructor signature is incorrect, the first argument must be:
在您的情况下File构造函数签名不正确,第一个参数必须是:
An Array of ArrayBuffer, ArrayBufferView, Blob, or DOMString objects — or a mix of any such objects. This is the file content encoded as UTF-8.
https://developer.mozilla.org/en-US/docs/Web/API/File/File#Parameters
ArrayBuffer、ArrayBufferView、Blob 或 DOMString 对象的数组——或任何此类对象的组合。这是编码为 UTF-8 的文件内容。
https://developer.mozilla.org/en-US/docs/Web/API/File/File#Parameters
new File([blob], "filename.json", {type: "text/json;charset=utf-8"});
回答by Sanat Gupta
I know I am late but I am facing the same issue so I thought should post my function it will help to convert BLOB to FILE. Thanks
我知道我迟到了,但我面临着同样的问题,所以我认为应该发布我的函数,它将有助于将 BLOB 转换为 FILE。谢谢
imageCropped(event: ImageCroppedEvent) {
this.Files = event.file;
let fileName = new Date().getTime() + ".jpeg";
let filedata = new File([this.Files], fileName, {
type: "image/jpeg",
lastModified: Date.now()
});
console.log(filedata);
}

