Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
zymr-keshav opened this issue Aug 4, 2017 · 5 comments
Closed

Comments

@zymr-keshav
Copy link

zymr-keshav commented Aug 4, 2017

Using macOS Sierra 10.12.5 and angular 1.6 and FileSaver v 1.3.2 and checking into Chrome 60

I need to download one HTTP response data ( object) as inputData as a json file and using below approach

using file type in Blob() and 2 parameters in saveAs() is working fine

var fileData = JSON.stringify(inputData, undefined, 4); // first use JSON.stringify 
var blob = new Blob([fileData], {type: "text/json;charset=utf-8"}); // save as Blob 
var fileName = "Report.json";
saveAs(blob, fileName);  

But why this is not working when we setting 3rd parameter as file type in File() and than save this ( as per suggested in README.md)

var file = new File(blob, fileName, {type: "text/json;charset=utf-8"});
saveAs(file);

nor even this way ( only write the file type in File() )

var saveText = JSON.stringify(inputData);
var file = new File(saveText, fileName, {type: "text/json;charset=utf-8"});
saveAs(file);

why we need to write file type in Blob() constructor not in File() constructor?

Kindly provide me some understanding here.

@jimmywarting
Copy link
Collaborator

jimmywarting commented Aug 4, 2017

http://caniuse.com/#feat=fileapi

You are likely testing this on old safari, IE or edge, they only partly support File object, you can get them from file input but not create them yourself

Oh, btw you have to use an array in the File constructor as well

new File([blob], ...)
new File([saveText], ...)

@zymr-keshav
Copy link
Author

I have checked and it says I can use FileSaver.js in my system. I have added as an array [saveText] because I thought it is necessary? isn't it?

@jimmywarting
Copy link
Collaborator

jimmywarting commented Dec 6, 2017

You can use filesaver, but you can't use the File constructor allways.

If you like you could use this to try and fix the constructor.

 // Fix so you can construct your own File
  try {
    new File([], '')
  } catch (a) {
    window.File = function(b, d, c) {
      var blob = new Blob(b, c)
      var t = c && void 0 !== c.lastModified ? new Date(c.lastModified) : new Date

      Object.defineProperties(blob, {
        name: {
          value: d
        },
        lastModifiedDate: {
          value: t
        },
        lastModified: {
          value: +t
        },
        toString: {
          value() {
            return '[object File]'
          }
        }
      })

      if (window.Symbol && Symbol.toStringTag) {
        Object.defineProperty(blob, Symbol.toStringTag, {
          value: 'File'
        })
      }

      return blob
    }
  }

@Ruffio
Copy link

Ruffio commented Mar 7, 2018

@zymr-keshav has this issue been resolved and can be closed?

@jimmywarting
Copy link
Collaborator

I think this will not be anything filesaver will take care of. I provided a polyfill for constructing a File but the safest way is to construct a Blob and send a filename (using both argument)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants