ie下ajaxSubmit上传文件出现下载提示框问题
用ajaxSubmit上传文件,谷歌和火狐没问题,ie下js回调函数无法接受,而是弹出下载提示,百度一圈没找到解决方案,FQ谷歌了终于找了,还是老外牛
<div class="col-sm-12"> <form id="filePost" action="/OM/Order/SaveFile" method="post" enctype="multipart/form-data" onsubmit="return false;"> <input type="file" name="qtFileName" style="display: inline-block;" ng-model="qtFile" /> @*<a class="btn btn-sm btn-info" ng-click="fileUpload()">保存</a>*@ <button class="btn btn-sm btn-info" ng-click="fileUpload()">保存</button> <input type="input" style="display:none;" name="roomId" ng-model="roomId" /> </form> </div>
原来后台直接返json代码,ie无法出现提示
public ActionResult SaveFile(HttpPostedFileBase qtFileName, Guid roomId) { MessageResult mr = new MessageResult(); if (qtFileName == null || qtFileName.ContentLength == 0) { mr.Message = "请选择要上传的文档"; return Json(mr); } string[] allowExt = new[] { ".xls", ".xlsx" }; var extention = Path.GetExtension(qtFileName.FileName); if (extention == "" || !allowExt.Contains(extention)) { mr.Message = "只允许上传.xls和.xlsx格式的Excel文档"; return Json(mr); } string basePath = Server.MapPath(Excel2QTsHelper.UploadFileVirtualPath); if (!Directory.Exists(basePath)) { Directory.CreateDirectory(basePath); } var realFileName = Path.GetFileNameWithoutExtension(qtFileName.FileName); var fileName = Path.Combine(basePath, roomId + extention); qtFileName.SaveAs(fileName); try { var orderVos = _excel2QTsHelper.Excel2QTs(roomId, null); bool result = _courseOrderProxy.CreateOrders(orderVos); if (result) { mr.Success = true; mr.Message = "上传成功"; return Json(mr, "text/html", System.Text.Encoding.UTF8, JsonRequestBehavior.AllowGet); } } catch (Exception e) { mr.Success = false; mr.Message =e.Message; return Json(mr, "text/html", System.Text.Encoding.UTF8, JsonRequestBehavior.AllowGet); } return Json(mr, "text/html", System.Text.Encoding.UTF8, JsonRequestBehavior.AllowGet); }
老外给出答案:
If anyone is using ASP.net MVC and trying to fix this issue - I used the following built in methods in the MVC framework. Simply update the content Type and encoding on the JsonResult.
public ActionResult Index(int id) { // Fetch some data var someData = GetSomeData(); // Return and update content type and encoding return Json(someData, "text/html", System.Text.Encoding.UTF8, JsonRequestBehavior.AllowGet); }
由于返回来的是字符串,所以还有转为json对象
$scope.fileUpload = function () { var options = { beforeSubmit: function () { }, error: function () { alert("上传失败"); }, success: function (data) { var data = $.parseJSON(data); if (data.Success) { alert("上传成功"); } else { alert(data.Message || "上传失败"); } } }; $('#filePost').ajaxSubmit(options); };
完美解决!