Question

I am uploading files using jQuery.ajax and everything works perfect in modern browsers like Google Chrome, Mozilla Firefox, Opera, except of Internet Explorer 10.

new FormData($('.uploadForm')[0]) doesnt work in IE10, but if I only try with this piece of code: new FormData($('.uploadForm')) it works...Looks like it does not accept elements at specific index or something? I dont understand this realy good, that is the reason, why I am searching for help.

Does it exist any kind of workaround for this example for IE10?

JS:

var form = new FormData($('.uploadForm')[0]);
config.progressBar.progressWidth = 0;
$('.uploadForm .valueBox').fadeOut('slow',function(){
    $(this).addClass('hidden')
    $('.meter').removeClass('hidden').width(config.progressBar.width);
    $.ajax({
        url: '../../uploads/some.php',
        type: 'POST',
        xhr: function() {
            var myXhr = $.ajaxSettings.xhr();
            if(myXhr.upload){
                myXhr.upload.onprogress = progress;
            }
            return myXhr;
        },
        success: function (res) {
            console.log(res)
        },
        data: form,
        cache: false,
        contentType: false,
        processData: false
    });

Peace of some.php code:

foreach($_FILES["file"]["error"] as $key => $value) {
    if ($value == UPLOAD_ERR_OK){

        $name = $_FILES["file"]["name"][$key];

        $arr_files = getimagesize($_FILES["file"]["tmp_name"][$key]); 
        $width = $arr_files[0];
        $height = $arr_files[1];
        $mime = $arr_files['mime'];

        copy($_FILES['file']['tmp_name'][$key], '../uploads/upload/'.$name);

        echo json_encode($_FILES);
    }
}

IE10 error thrown: SCRIPT5: Access is denied.

Was it helpful?

Solution

Don't pass the files into the constructor, but use append, like:

var formData = new FormData();
formData.append('file', $('input[type=file]')[0].files[0]);

OTHER TIPS

This function is working good if need to try this,

Action url code don't given here, just php file upload function but return only -1,0,1,2 for identify the error and success

function submitForm(){
    $('.uploading').show();
    var formObj = $('#multiform');
    var formURL = formObj.attr("action");
    if(window.FormData !== undefined)  // for HTML5 browsers
    {

        var formData = new FormData(formObj[0]);
        $.ajax({
            url: formURL,
            type: 'POST',
            data:  formData,`enter code here`
            mimeType:"multipart/form-data",
            contentType:  false,
            cache: false,
            processData:false,
            success: function(data, status){

               $('.uploading').hide();
               if(data==1){
                $('.uploadSuccessReport').show();
                $(".uploadSuccessReport").html('Successfully uploaded');
                setTimeout(function() { $('.uploadSuccessReport').hide('slow'); }, 2000);
              }else if(data==0){
                $('.uploadErrorReport').show();
                $(".uploadErrorReport").html('Something Error Happen');
               setTimeout(function() { $('.uploadErrorReport').hide('slow'); }, 2000);
              }else if(data==-1){
                $('.uploadErrorReport').show();
                $(".uploadErrorReport").html('Already exists, Please chose another one or Rename');
                setTimeout(function() { $('.uploadErrorReport').hide('slow'); }, 2000);
              } else if(data==2){
                $('.uploadErrorReport').show();
                $(".uploadErrorReport").html('Invalid File, Please Check file Format');
                setTimeout(function() { $('.uploadErrorReport').hide('slow'); }, 2000);
              } 
          },
      });
    }
} 


<form name="imageUpload" action="ajaxupload.php" method="post" id="multiform" enctype="multipart/form-data">
<span class="btn btn-file uploadSpan">Upload <span class="uploading" style="display:none;"><img src="assets/ajax-loader.gif" width="15%"></span>
<input type="file" name="file" id="disabledInput" class="btn-file uploadImage" onchange="submitForm()" />
</span>
</form>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top