【问题标题】:Failed to construct 'File': Iterator getter is not callable in chrome 60 when use JSON.stringify()无法构造“文件”:使用 JSON.stringify() 时,无法在 chrome 60 中调用迭代器 getter
【发布时间】:2017-08-04 17:33:17
【问题描述】:

系统配置:macOS Sierra 10.12.5;铬 60;

我正在尝试将 JSON 数据(响应对象)下载为 json 文件,但是当我尝试使用浏览器 File() 对象来实现这一点时,它给出了错误

Failed to construct 'File': Iterator getter is not callable.

下面是我的代码

//`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);

是什么问题以及为什么我会收到此错误。当我使用JSON.stringify(inputData, undefined, 4); 时,它不会出错并在控制台中显示正确的对象。

【问题讨论】:

    标签: javascript fileapi


    【解决方案1】:

    在您的情况下File 构造函数签名不正确,第一个参数必须是:

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

    【讨论】:

    【解决方案2】:

    我知道我迟到了,但我面临同样的问题,所以我认为应该发布我的函数,它将有助于将 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);
      }
    

    【讨论】:

      猜你喜欢
      • 2019-08-01
      • 2013-10-19
      • 2018-06-02
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 2021-02-12
      • 2015-06-27
      • 1970-01-01
      相关资源
      最近更新 更多