【问题标题】:Is the FormData object available in Internet Explorer 10?Internet Explorer 10 中是否提供 FormData 对象?
【发布时间】:2012-12-28 06:47:12
【问题描述】:

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

这个脚本在所有浏览器中都非常好用,除了,猜猜是谁,Internet Explorer...

所以我做的第一件事是使用 AjaxForm Plugin for jQuery 为 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 中出现“拒绝访问”异常?

【问题讨论】:

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


    【解决方案1】:

    https://stackoverflow.com/a/13657047/641293https://stackoverflow.com/a/4335390/641293 可能有用。 IE 对您可以通过编程方式使用 &lt;input type='file'&gt; 执行的操作非常严格。

    基于第一个,将第一行更改为此修复问题吗?

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

    【讨论】:

    • 这个解决方案非常适合 IE10,但我仍然对 IE9 有一些问题——但我想我必须找到另一个解决方案,而不是“FormData Obj”
    【解决方案2】:

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

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

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

    【讨论】:

    • 这对于以前的控件来说没问题,但我需要一些东西来防止 IE10 中的错误 @Alex 编写了正确的解决方案。无论如何,谢谢 +1
    猜你喜欢
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 2013-04-10
    相关资源
    最近更新 更多