javascript FormData 在 Internet Explorer 中不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9573607/
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-26 07:05:42  来源:igfitidea点击:

FormData not working in Internet Explorer?

javascripthtmlajaxxmlhttprequestform-data

提问by Jake

    function uploadPhoto(file) {
    if (!file || !file.type.match(/image.*/)){
        if(!file){
            postStatus();
        } else {
            return;
        }
    }
    var fd = new FormData();
    fd.append("image", file);
    fd.append("privacy", document.getElementById('privacy-handler').value);
    var xhr = GetXmlHttpRequest(); 
    xhr.open("POST", "url here");
    slideUp('photo-upload');
    slideDown('photo-manager-txt');
    document.getElementById("photo-manager-txt").innerHTML='<i>Please wait a moment while we process your photo.</i>';
    xhr.onload = function() {
        if(xhr.responseText == '0'){
            document.getElementById('photo-manager-txt').innerHTML='<br />Photo upload failed';
            slideDown('photo-upload');
            return;
        } else {
            document.getElementById('photo-txt').value='grab?v=1&file='+xhr.responseText;
            document.getElementById('photo-manager-txt').innerHTML='Photo uploaded and shared.';
            postStatus();
        }
    }
    xhr.send(fd);
}

This function seems not to be working. When I call the function I'm using:

此功能似乎不起作用。当我调用我正在使用的函数时:

onClick="uploadPhoto(document.getElementById('ID-HERE').files[0]);"

When I remove the 0from files[], it atleast runs postStatus();, but it won't upload the photo. How do I go about fixing this?

当我删除0from 时files[],它至少会运行postStatus();,但不会上传照片。我该如何解决这个问题?

回答by Samuel Zhang

The XHR in IE doesn't support FormData until IE10. You may install Windows 8 Customer Preview to have a try.

IE 中的 XHR 直到 IE10 才支持 FormData。您可以安装 Windows 8 Customer Preview 来试一试。