Internet Explorer 10中是否提供FormData对象?

时间:2013-01-14 09:57:38

标签: javascript ajax html5 internet-explorer file-upload

我正在编写一个小型JavaScript应用程序,允许我异步上传图像。

这个脚本在每个浏览器中都很棒,除了猜猜谁是Internet Explorer ......

所以我做的第一件事是使用Ajax的AjaxForm插件为IE9版本创建一个后备版本,这非常有效!

这是JS脚本。

$("#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和其他所有变量都在全局脚本中定义,但不要担心,它们可以正常工作。确切地说,WindowApi就是这样:

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;};

所以,有了这一堆代码,我处理每个浏览器和IE9浏览器......

问题现在是IE10,因为它已经获得了所有window.*方法,并且它可以使用FormData对象。但是当我尝试使用IE10和FormData上传内容时,我收到了formData对象的“Access Is Denied”错误。

此过程涉及的HTML是:

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

所以最后我的问题是:

在尝试访问FormData对象时,如何避免在IE10中出现“拒绝访问”异常?

2 个答案:

答案 0 :(得分:7)

https://stackoverflow.com/a/13657047/641293https://stackoverflow.com/a/4335390/641293可能有用。 IE对于使用<input type='file'>以编程方式执行的操作非常严格。

基于第一个,是否将第一行更改为此修复内容?

$("#Uploader").on('click', function(e){ /* rest of the function unchanged... */

答案 1 :(得分:1)

当您提交包含已被javascript搞乱的字段的表单时,您会收到拒绝访问权限。您在上传字段中动态添加了disabled属性,这可能是您收到Access denied的原因。也许你应该在没有change事件禁用字段的情况下试一试?

顺便说一句,您可能最好结合FormData检查File API的可用性:

var formDataSupport = false;
if (typeof FormData === 'function' && 
    window.File && 
    window.FileReader && 
    window.FileList && 
    window.Blob)
{
  console.log("File API available, formData available");  
  formDataSupport = true; 
}