Javascript Iframe文件上传在IE上的工作
我想用ajax上传文件。 所以我有一个小的jquery插件,可以在页面上添加一个按钮。单击按钮后,将打开一个带有表单和文件输入的引导对话框。表单提交给ASP MVC方法,该方法保存文件并返回json,如alert所示 它在chrome中运行良好,但在IE中,不会调用iframe的onload函数,浏览器会提供json供我下载。 服务器端:Javascript Iframe文件上传在IE上的工作,javascript,jquery,internet-explorer,iframe,Javascript,Jquery,Internet Explorer,Iframe,我想用ajax上传文件。 所以我有一个小的jquery插件,可以在页面上添加一个按钮。单击按钮后,将打开一个带有表单和文件输入的引导对话框。表单提交给ASP MVC方法,该方法保存文件并返回json,如alert所示 它在chrome中运行良好,但在IE中,不会调用iframe的onload函数,浏览器会提供json供我下载。 服务器端: public ActionResult Upload(IEnumerable<HttpPostedFileBase> file) {
public ActionResult Upload(IEnumerable<HttpPostedFileBase> file)
{
return Json(new { FileName = file.First().FileName });
}
公共操作结果上传(IEnumerable文件)
{
返回Json(新的{FileName=file.First().FileName});
}
客户端:
$.fn.fileUpload = function (options) {
//options
var settings = $.extend({
inputName: 'file'
}, options);
//variables
var that = this;
var index = 0;
var addFileBtn = $("<button>+</button>");
var modal = $('<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">'+
'<div class="modal-dialog" role="document">'+
'<div class="modal-content">'+
'<div class="modal-header">'+
'<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>'+
'<h4 class="modal-title" id="myModalLabel">Modal title</h4>'+
'</div>'+
'<div class="modal-body">' +
'<iframe name="postiframe" id="postiframe" style="width:0;height:0;border:0px solid #fff"></iframe>' +
'<form id="my_form" name="my_form" action="/Home/Upload" method="POST" enctype="multipart/form-data" target="postiframe">' +
'<input name="complaintId" value="123" type="hidden" />'+
'<input name="file" id="file" type="file"/>'+
'<input type="submit" value="Submit" id="submitBtn"/>'+
'</form>'+
'</div>'+
'<div class="modal-footer">'+
'<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>'+
'<button type="button" class="btn btn-primary">Save changes</button>'+
'</div>'+
'</div>'+
'</div>' +
'</div>');
//init
addFileBtn.click(function () {
modal.modal();
modal.find("#submitBtn").click(function (ev) {
var form = modal.find('#my_form');
form.attr("encoding", "multipart/form-data");
form.attr("enctype", "multipart/form-data");
modal.find("#postiframe").load(function () {
var iframeContents = $(this).contents().find("pre").html()
alert(iframeContents);
});
form.submit();
return false;
});
});
that.append(addFileBtn);
}
$.fn.fileUpload=函数(选项){
//选择权
变量设置=$.extend({
inputName:'文件'
},选项);
//变数
var=这个;
var指数=0;
var addFileBtn=$(“+”);
变量模态=$('')+
''+
''+
''+
“×;”+
“情态标题”+
''+
'' +
'' +
'' +
''+
''+
''+
''+
''+
''+
“结束”+
“保存更改”+
''+
''+
'' +
'');
//初始化
addFileBtn.单击(函数(){
modal.modal();
模态。查找(“#submitBtn”)。单击(函数(ev){
var form=modal.find('my#u form');
attr(“编码”、“多部分/表单数据”);
form.attr(“enctype”、“多部分/表单数据”);
modal.find(“#positframe”).load(函数(){
var iframeContents=$(this.contents().find(“pre”).html()
警报(iFrameContent);
});
表单提交();
返回false;
});
});
append(addFileBtn);
}
正如epascarello建议的那样,我将Asp Mvc方法更改为返回内容而不是Json
public ActionResult Upload(IEnumerable<HttpPostedFileBase> file)
{
return Content("some string in json format");
}
公共操作结果上传(IEnumerable文件)
{
返回内容(“json格式的字符串”);
}
现在,内容被加载到iframe中,而不是下载提示符。您正在提交表单并返回JSON。一些浏览器将JSON显示为纯文本,其他浏览器则要求下载。听起来您不想返回JSON。谢谢。将ASP方法更改为返回内容(字符串内容)而不是Json,看起来还可以。如果你写下来作为答复,我会接受的。