我很想用ajax上传图片,所以我用的是FormData,但它对IE8不起作用。我已经看过了,它不可能在IE8上使用FormData,但我发现我没有什么可以替代的,以使它在IE8和其他浏览器上工作。有没有人能告诉我该怎么做,怎么做?
我正在尝试提交的表单
<form id="addImgForm" name="addImgForm" method="post" action="#URL(Action('ChiliTest-ImageUpload'))#" enctype="multipart/form-data">
<input id="newImage" type="file" name="newImage">
<input type="hidden" name="MAX_FILE_SIZE" value="12345">
<span id="addImage" class="button-addImage" type="submit"><isTradConstant keyword="l_customizationsChiliEditor_AddImageButtonTitle" template="CustomizationsChiliEditor" init="Ajouter"></span>
</form>
在addImgForm提交时调用
$.ajax({
url: myUrl,
type: "POST",
data: new FormData($(this).parent()[0]),
contentType : false,
async: false,
processData: false,
cache: false,
success: function(data) {
//do something
}
});
return false;
发布于 2014-12-31 15:59:07
理想情况下,当我遇到这个问题时,我会在浏览器中检查FormData,如果返回的结果是undefined,那么我就会通过iframe提交表单。
发布于 2014-12-31 12:39:42
你可以在IE8中使用jQuery表单插件通过ajax上传文件,你的示例代码应该是这样的:
1
$(document).ready(function() {
var options = {
beforeSend: function() {
$("#progress").show();
//clear everything
$("#bar").width('0%');
$("#message").html("");
$("#percent").html("0%");
},
uploadProgress: function(event, position, total, percentComplete) {
$("#bar").width(percentComplete + '%');
$("#percent").html(percentComplete + '%');
},
success: function() {
$("#bar").width('100%');
$("#percent").html('100%');
},
complete: function(response) {
$("#message").html("<font color='green'>" + response.responseText + "</font>");
},
error: function() {
$("#message").html("<font color='red'> ERROR: unable to upload files</font>");
}
};
$("#myForm").ajaxForm(options);
});
<script src="http://malsup.github.io/min/jquery.form.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<form id="myForm" action="/demo/Upload" method="post" enctype="multipart/form-data">
<input type="file" size="60" name="myfile">
<input type="submit" value="Ajax File Upload">
</form>
<div id="progress">
<div id="bar"></div>
<div id="percent">0%</div>
</div>
<br/>
<div id="message"></div>
发布于 2016-01-21 14:45:12
我们已经使用jquery plugin做了同样的事情,并且解决了这个问题。
它太简单了,就用吧。
$('#myForm').ajaxForm(function() {
});
它自动设置所有选项,而不是下面调用。
$.ajax({
url: myUrl,
type: "POST",
data: new FormData($(this).parent()[0]),
contentType : false,
async: false,
processData: false,
cache: false,
success: function(data) {
//do something
}
});
希望这将工作,让我知道如果在实施过程中的任何障碍。确保在使用ajaxform函数之前添加了jquery插件。不需要为其他浏览器做任何事情,它对IE和其他两者都有效。
https://stackoverflow.com/questions/27702418
复制相似问题