我使用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.
答案 0 :(得分:6)
不要将文件传递给构造函数,而是使用append
,例如:
var formData = new FormData();
formData.append('file', $('input[type=file]')[0].files[0]);
答案 1 :(得分:4)
如果需要尝试此功能,此功能正常,
动作网址代码这里没有给出,只是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>