40

System Configuration : macOS Sierra 10.12.5 ; chrome 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

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.

Pavlo
  • 43,301
  • 14
  • 77
  • 113
xkeshav
  • 53,360
  • 44
  • 177
  • 245

2 Answers2

76

In your case File constructor signature is incorrect, the first argument must be:

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

new File([blob], "filename.json", {type: "text/json;charset=utf-8"});
Pavlo
  • 43,301
  • 14
  • 77
  • 113
  • 3
    Looks like same applies to `new Blob(...)` as well. Thanks for the explicit example. – Nae Jul 31 '18 at 12:02
  • can you answer this https://stackoverflow.com/questions/65541545/getting-failed-to-execute-createobjecturl-on-url-overload-resolution-failed – Nithin Paul Jan 02 '21 at 16:59
1

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

 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);
  }
Sanat Gupta
  • 1,086
  • 1
  • 10
  • 16