FormData文件,ajax不会在IE 11中发布$ _FILES

时间:2015-04-29 03:03:36

标签: javascript php form-data jqxhr jquery-html5uploader

我在JavaScript中使用以下代码(formdata),并且在Mozilla和Chrome中正常工作。当我在 IE 11 中尝试时,它使用ajax不能很好地发布。调用success函数,但$_FILES在服务器端为空。

file = _files[i][j];
if(j<_files[i].length){

    if(file){
    var data = new FormData(); 
    data.append("uploadedimages", file);
    console.log("formdata:"+data);
    progressElement = $('#divimg_'+i+'_'+j);
    progressElement.css('visibility','visible').hide().fadeIn('500');

    $.ajax({
        url: "<?php echo base_url();?>"+"upload/do_upload",
        data: data,
        cache: false,
        contentType: false,
        processData: false,
        type: 'POST',
        success: function(data){
            console.log(data);
            j++;
            if(j<_files[i].length){
                uploadmore(i,j);
            }else{
                i++;
                uploadme(i,0);
            } 
        },
        xhr: function()
        {
            var xhr = new window.XMLHttpRequest();
            xhr.upload.addEventListener( 'progress', function( e )
            {
                if( e.lengthComputable )
                {
                    // Append progress percentage.
                    var progressValue = ( e.loaded / e.total ) * 100;
                    console.log( i + ':' + progressValue );
                    progressElement.find( 'input' ).val( progressValue ).change();

                    // Listen for clicks on the cancel icon.
                    $('#stopupload').click( function()
                    {
                        if( progressElement.hasClass( 'working' ) )
                        {
                            xhr.abort();
                            progressElement.fadeOut(500);
                        }
                    });

                    if( progressValue == 100 )
                    {
                        progressElement.removeClass( 'working' );
                    }
                }
            }, false);
            return xhr;
        }
    });  
    } 
    else{
        console.log("FILE ERROR!");
        j++;
        uploadmore(i,j);
        }
}

1 个答案:

答案 0 :(得分:0)

我有同样的问题。经过数天的尝试各种参数后,我将它与IE 11一起使用。请确保将form.serialize()添加到数据组件中。我还添加了接收网址的完整网址

data        : formdata ? formdata : form.serialize(),

        $('#btnUploadFile').on('click', function () {

                var formdata = new FormData();
                var files = $("#fileUpload").get(0).files;

                // Add the uploaded image content to the form data collection
                if (files.length > 0) {
                    formdata.append("UploadedImage", files[0]);
                }
                else
                {
                    alert("Please select a file to upload.")
                    return;
                }

                var loc = window.location;
                var uploadReceiverURL;

                if (loc.host.indexOf("local")>-1)
                {
                    uploadReceiverURL = loc.protocol + '//' + loc.host + "/uploadReceiver.aspx";
                }
                else
                {
                    uploadReceiverURL = loc.protocol + '//' + loc.host +  "/rolechanges/uploadReceiver.aspx";
                }

                console.log('uploadReceiverURL: ' + uploadReceiverURL);
                var ajaxRequest =  $.ajax({
                    url         : uploadReceiverURL,
                    data        : formdata ? formdata : form.serialize(),
                    cache       : false,
                    contentType : false,
                    processData : false,
                    type        : 'POST',
                    success     : function(data, textStatus, jqXHR) 
                    {
                        console.log('Success: ' + textStatus);
                    },
                    error: function(jqXHR, textStatus, errorThrown)
                    {
                        console.log('ERRORS: ' + textStatus);
                    } 
                });
                ajaxRequest.done(function (xhr, textStatus) {      
                    document.getElementById('fileUpload').value = '';
                });
            });

        })