簡體   English   中英

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

[英]Is the FormData object available in Internet Explorer 10?

我正在編寫一個小的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中出現“拒絕訪問”異常?

https://stackoverflow.com/a/13657047/641293https://stackoverflow.com/a/4335390/641293可能會有用。 對於使用<input type='file'>編程方式執行的操作,IE非常嚴格。

基於第一個,是否更改了第一行來修復這個問題?

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

當您提交包含已被javascript搞亂的字段的表單時,您將獲得拒絕訪問權限。 您在uploadfield上動態添加了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; 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM