[英]FormData of element not working - Internet Explorer 10
我使用jQuery.ajax
上传文件,除了Internet Explorer 10之外,一切在Google Chrome,Mozilla Firefox,Opera等现代浏览器中都很完美。
new FormData($('.uploadForm')[0])
在IE10中不起作用,但如果我只尝试使用这段代码: new FormData($('.uploadForm'))
它可以工作......看起来像它不接受特定指数或其他东西的元素? 我不明白这真的很好,这就是为什么我在寻求帮助的原因。
IE10的这个例子是否存在任何变通方法?
JS:
var form = new FormData($('.uploadForm')[0]);
config.progressBar.progressWidth = 0;
$('.uploadForm .valueBox').fadeOut('slow',function(){
$(this).addClass('hidden')
$('.meter').removeClass('hidden').width(config.progressBar.width);
$.ajax({
url: '../../uploads/some.php',
type: 'POST',
xhr: function() {
var myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){
myXhr.upload.onprogress = progress;
}
return myXhr;
},
success: function (res) {
console.log(res)
},
data: form,
cache: false,
contentType: false,
processData: false
});
和平的some.php
代码:
foreach($_FILES["file"]["error"] as $key => $value) {
if ($value == UPLOAD_ERR_OK){
$name = $_FILES["file"]["name"][$key];
$arr_files = getimagesize($_FILES["file"]["tmp_name"][$key]);
$width = $arr_files[0];
$height = $arr_files[1];
$mime = $arr_files['mime'];
copy($_FILES['file']['tmp_name'][$key], '../uploads/upload/'.$name);
echo json_encode($_FILES);
}
}
抛出IE10错误: SCRIPT5: Access is denied.
不要将文件传递给构造函数,而是使用append
,如:
var formData = new FormData();
formData.append('file', $('input[type=file]')[0].files[0]);
如果需要尝试这个功能,这个功能很好用,
这里没有给出动作url代码,只是php文件上传功能但只返回-1,0,1,2,用于识别错误和成功
function submitForm(){
$('.uploading').show();
var formObj = $('#multiform');
var formURL = formObj.attr("action");
if(window.FormData !== undefined) // for HTML5 browsers
{
var formData = new FormData(formObj[0]);
$.ajax({
url: formURL,
type: 'POST',
data: formData,`enter code here`
mimeType:"multipart/form-data",
contentType: false,
cache: false,
processData:false,
success: function(data, status){
$('.uploading').hide();
if(data==1){
$('.uploadSuccessReport').show();
$(".uploadSuccessReport").html('Successfully uploaded');
setTimeout(function() { $('.uploadSuccessReport').hide('slow'); }, 2000);
}else if(data==0){
$('.uploadErrorReport').show();
$(".uploadErrorReport").html('Something Error Happen');
setTimeout(function() { $('.uploadErrorReport').hide('slow'); }, 2000);
}else if(data==-1){
$('.uploadErrorReport').show();
$(".uploadErrorReport").html('Already exists, Please chose another one or Rename');
setTimeout(function() { $('.uploadErrorReport').hide('slow'); }, 2000);
} else if(data==2){
$('.uploadErrorReport').show();
$(".uploadErrorReport").html('Invalid File, Please Check file Format');
setTimeout(function() { $('.uploadErrorReport').hide('slow'); }, 2000);
}
},
});
}
}
<form name="imageUpload" action="ajaxupload.php" method="post" id="multiform" enctype="multipart/form-data">
<span class="btn btn-file uploadSpan">Upload <span class="uploading" style="display:none;"><img src="assets/ajax-loader.gif" width="15%"></span>
<input type="file" name="file" id="disabledInput" class="btn-file uploadImage" onchange="submitForm()" />
</span>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.