Failed to create File: when using JSON.stringify ()

System Configuration: macOS Sierra 10.12.5; chrome 60;

I am trying to load JSON (response object) data as a json file, but when I tried to achieve this using the File () object of the browser, it gives an error

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 problem and why am I getting this error. when I use JSON.stringify(inputData, undefined, 4);, then it gives no errors and displays the correct object in the console.

+14
source share
2 answers

In your case, the Fileconstructor signature is incorrect, the first argument should be:

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

, , , , , BLOB FILE.

 imageCropped(event: ImageCroppedEvent) {
    this.croppedImage = event.base64;
    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);
  }
0

All Articles