FormData in IE 11 is undefined

I have the following script to get the file data from the input file type:

var uploadfiles = $("#upFile").get(0);
var uploadedfiles = uploadfiles.files;

var fromdata = new FormData();
for (var i = 0; i < uploadedfiles.length; i++) {
    fromdata.append(uploadedfiles[i].name, uploadedfiles[i]);
}

// ajax code omitted that uploads file

      

This works fine in all browsers I've tested with except IE 11. In this, it doesn't understand what FormData () is? I have currently read a few different workarounds, but NONE of them work, no matter how hard you try, you can get the file information from the input. Anyone else could help this? Even if I try to use jQuery to get the object, then for some reason the "files" are undefined.

EDIT: read more on the internet, it looks like it might be because IE doesn't provide input access until the form is submitted, however I am using ajax to upload the file so I cannot submit it.

EDIT2: I should also mention that this code is called on the file input change event, not sure if it has any meaning, but it is best to mention it

+4


source to share


2 answers


After checking the docmode in IE developer tools it turned out that it was reverted to 9 for some reason, I had an old meta tag for X-UA-Compatible on my main page which I changed to:

<meta http-equiv="X-UA-Compatible" content="IE=edge">

      



FormData had no problem at all.

+8


source


FormData is not fully supported in IE11.

To be specific: the FormData instance does not have a 'set' method. Instead, you need to use the append method as shown below:

const formData = new FormData();
formData.append('your_key_name', 'your_value_goes_here');

      



Setting the meta http equivalent to IE = edge will not make the 'set' property work and is unnecessary.

Link: https://developer.mozilla.org/en-US/docs/Web/API/FormData/append

0


source







All Articles