Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the FormData object available in Internet Explorer 10?

I'm writing a little JavaScript application that allows me to upload images asynchronously.

This script works awesome in every browser except for, guess who, Internet Explorer...

So the first thing that I made is to create a fallback for IE9- versions with AjaxForm Plugin for jQuery, which works great!

Here's the JS script.

$("#Uploader").change(function(e){
        var form = $("#UploaderForm");
        form.trigger('submit');
        $(this).attr('disabled','disabled');
        e.preventDefault();
});
$("#UploaderForm").submit(function(e){
        e.preventDefault();
        e.stopPropagation();
        var type="POST";var loading=$("#PhotoIsLoading");
        if(windowApi === true){
            var formData = new FormData($(this)[0]);
            $.ajax({
                url: url,
                type: type,
                xhr: function() {
                    myXhr = $.ajaxSettings.xhr();
                    if(myXhr.upload){ myXhr.upload.addEventListener('progress',progressHandlingFunction, false);}
                    return myXhr;
                },
                beforeSend: function(){loading.removeClass('isHidden_important');},
                success: function(response){
                    jres = JSON.parse(response);
                    alert("Test ok, file uploaded");
                },
                error: function(response){console.warn(response);},
                data: formData, 
                cache: false,
                contentType: false,
                processData: false
            });
            e.preventDefault();
        }else{
            $(this).ajaxSubmit({
                url: url,
                dataType: 'json',
                type: type,
                beforeSubmit: function(){loading.removeClass('isHidden_important');$(this).formSerialize();},
                success:function(response){
                    jres = JSON.parse(response);
                    alert("FallbackTest Complete");
                },
                error: function(response){console.warn(response);},
            });
            e.preventDefault();
            return false;
        }
    });

WindowApi and every other variable are defined in a global script but don't worry, they work. To be precise, WindowApi is this:

var windowApi=true;
if(window.File&&window.FileReader&&window.FileList&&window.Blob){
console.log("window.api ready");windowApi=true;}
else{console.log("window.api not ready");windowApi=false;};

So, with this bunch of lines of code I handle every browser and IE9- browsers...

The problem now is with IE10 because it has got all the window.* methods and it can use the FormData object. But when I try to upload something with IE10 and FormData I receive an "Access Is Denied" error for the formData object.

The HTML that is involve in this process is:

<form name="UploaderForm" id="UploaderForm" method="post" enctype="multipart/form-data">
    <input type="file" name="Uploader" id="Uploader" accept="image/*" tabindex="1" />
</form>

So at the end my question is:

How can I avoid getting an "Access Denied" exception in IE10 when trying to access the FormData Object?

like image 586
Ludo237 Avatar asked Jan 14 '13 09:01

Ludo237


People also ask

How do I get FormData?

get() The get() method of the FormData interface returns the first value associated with a given key from within a FormData object. If you expect multiple values and want all of them, use the getAll() method instead. Note: This method is available in Web Workers.

What is FormData object in JS?

In JavaScript, a FormData object is a common way to create a bundle of data to send to the server using XMLHttpRequest or fetch. It replicates the functionality of the HTML form element. We can think of it as an array of arrays. There is one array for each element that we want to send to the server.

What format is FormData?

What's FormData. FormData is simply a data structure that can be used to store key-value pairs. Just like its name suggests it's designed for holding forms data i.e you can use it with JavaScript to build an object that corresponds to an HTML form.


1 Answers

https://stackoverflow.com/a/13657047/641293 and https://stackoverflow.com/a/4335390/641293 might be useful. IE is quite strict about what you can do with <input type='file'> programmatically.

Based on the first one, does changing the first line to this fix things?

$("#Uploader").on('click', function(e){ /* rest of the function unchanged... */
like image 99
Alex Ghiculescu Avatar answered Oct 10 '22 06:10

Alex Ghiculescu