javascript 元素的 FormData 不起作用 - Internet Explorer 10

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18785024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:13:59  来源:igfitidea点击:

FormData of element not working - Internet Explorer 10

javascriptphpjqueryinternet-explorerform-data

提问by Clem

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

我正在使用上传文件,jQuery.ajax并且在现代浏览器(例如 Google Chrome、Mozilla Firefox、Opera)中一切正常,但 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.

new FormData($('.uploadForm')[0])在 IE10 中不起作用,但如果我只尝试使用这段代码:new FormData($('.uploadForm'))它有效......看起来它不接受特定索引处的元素或其他什么?我不明白这真的很好,这就是我寻求帮助的原因。

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

对于 IE10 的这个示例,它是否存在任何类型的解决方法?

JS:

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.phpcode:

some.php代码和平:

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.

IE10 错误抛出: SCRIPT5: Access is denied.

回答by Prisoner

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

不要将文件传递给构造函数,而是使用append,例如:

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

回答by shaheb

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

这里不给出动作url代码,只是php文件上传功能,但只返回-1,0,1,2以识别错误和成功

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>