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

as file name is Blob in IE #43

Closed
mmiklosik opened this issue Nov 29, 2017 · 14 comments
Closed

as file name is Blob in IE #43

mmiklosik opened this issue Nov 29, 2017 · 14 comments

Comments

@mmiklosik
Copy link

mmiklosik commented Nov 29, 2017

if i append File to FormData and after get filename

formdata.get("file").name

i get Blob

...
i found

if (!window.FormData || !window.FormData.prototype.keys) {
  var x = function(b, a, d) {
      if (2 > arguments.length)
        throw new TypeError(
          "2 arguments required, but only " + arguments.length + " present."
        );
      return a instanceof Blob
        ? [
            b + "",
            a,
            void 0 !== d
              ? d + ""
              : "File" === Object.prototype.toString.call(a).slice(8, -1)
                ? a.name
                : "Blob"
          ]
        : [b + "", a + ""];
    },

i print value of param a is [object File], but Object.prototype.toString.call(a) is [object Blob]

if return change to

return a instanceof Blob
        ? [
            b + "",
            a,
            void 0 !== d
              ? d + ""
              : a.name
          ]
        : [b + "", a + ""];

and i get real name

@mmiklosik mmiklosik changed the title as file name in ] as file name is Blob in IE Nov 29, 2017
@jimmywarting
Copy link
Owner

jimmywarting commented Nov 29, 2017

Old ie don't have Symbol.toStringTag and the polyfill was therefore not able to change the Object.prototype.toString.call to return correct type of the polyfilled File constructor.

I changed the way it checks if it's a "File" so it return the correct filename.

Maybe cold have be fixed if the Symbol polyfill was included, but not sure. Could be nice to include since it contains toStringTag and entries which formdata also uses to be able to do [...fd] and for...of

@mmiklosik
Copy link
Author

still doesn't work ( [object File].toString() is [object Blob] :( & i don't know why

it would by possible instead of this part

    return value instanceof Blob
      // normalize name and filename if adding an attachment
      ? [name + '', value, filename !== undefined
        ? filename + '' // Cast filename to string if 3th arg isn't undefined
        : type(value) === 'File' // if it's a File
          ? value.name // Use File.name
          : 'Blob'] // otherwise fallback to Blob

      // If no attachment, just cast the args to strings
      : [name + '', value + '']

give

    return value instanceof Blob
      // normalize name and filename if adding an attachment
      ? [name + '', value, filename !== undefined
        ? filename + '' // Cast filename to string if 3th arg isn't undefined
        : value.name // if object (File) have name prop
          ? value.name // Use File.name
          : 'Blob'] // otherwise fallback to Blob

      // If no attachment, just cast the args to strings
      : [name + '', value + '']

@jimmywarting
Copy link
Owner

jimmywarting commented Dec 6, 2017

Guess we could take the name property without checking if it's a file
Don't think it will do any harm

But i will change it to this:

    return value instanceof Blob
      // normalize name and filename if adding an attachment
      ? [name + '', value, filename !== undefined
        ? filename + '' // Cast filename to string if 3th arg isn't undefined
        : value.name || 'Blob'] // otherwise use File's name or fallback to Blob

      // If no attachment, just cast the args to strings
      : [name + '', value + '']

@mmiklosik
Copy link
Author

nice, thanks

@jimmywarting
Copy link
Owner

jimmywarting commented Dec 6, 2017

or maybe not...

Trying this out in chrome yeilds file.name == ''

fd = new FormData()
fd.append('f', new File([], ''))
console.log([...fd][0][1])

@jimmywarting
Copy link
Owner

jimmywarting commented Dec 6, 2017

maybe need to check at least if it's a string
So...

    return value instanceof Blob
      // normalize name and filename if adding an attachment
      ? [name + '', value, filename !== undefined
        ? filename + '' // Cast filename to string if 3th arg isn't undefined
        : typeof value.name === 'string' // if object (File) have name prop
          ? value.name // Use File.name
          : 'Blob'] // otherwise fallback to Blob

@mmiklosik
Copy link
Author

mmiklosik commented Dec 6, 2017

this solution

...
: typeof value.name === 'string'
...

it works in both chrome and ie, for me

jimmywarting added a commit that referenced this issue Dec 6, 2017
@jimmywarting
Copy link
Owner

There u go, publish 3.0.9

@jimmywarting
Copy link
Owner

jimmywarting commented Dec 6, 2017

Curious doe...

still doesn't work ( [object File].toString() is [object Blob] :( & i don't know why

What did you do to make this happened? Would like a to build a test for this.
Guessing you tried to construct the file?

@jimmywarting
Copy link
Owner

Dose this fail to override toString in IE?

var blob = new Blob()

Object.defineProperties(blob, {
  toString: {
    value: function() {
      return '[object File]'
    }
  }
})

console.log(blob.toString())

@mmiklosik
Copy link
Author

it finally works, thank you so much ;)

@mmiklosik
Copy link
Author

about that toString() problem, i thing that problem IE
if i upload File is type object File but toString make him blob

@jimmywarting
Copy link
Owner

lol, tried it myself in browserstack so wierd

@mmiklosik
Copy link
Author

sometimes, whole IE is weird :)

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

2 participants