gpt4 book ai didi

javascript - FormData 对象在 Internet Explorer 10 中可用吗?

转载 作者:技术小花猫 更新时间:2023-10-29 12:34:11 27 4
gpt4 key购买 nike

我正在编写一个允许我异步上传图像的小 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 对象的“访问被拒绝”错误。

此过程中涉及的 HTML 是:

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

最后我的问题是:

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

最佳答案

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... */

关于javascript - FormData 对象在 Internet Explorer 10 中可用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14316240/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com