天天看點

ajaxFileUpload上傳檔案後提示下載下傳的問題

在某些版本浏覽器下ajaxFileUpload上傳檔案會提示下載下傳,

1:為什麼?

可以觀察到,即便傳回 JsonResult 在傳回的頭中也沒有任何消息體,直接了解為文本了。

2:解決方案

前端:

function uploadImg(fimgi) {     if (("#fimg" + fimgi).val().length > 0) {         //alert(("#fimg" + fimgi).val().length > 0) {         //alert(("#fimg" + fimgi).val().length);     }     else {         alert("請選擇圖檔");         return;     .ajaxFileUpload({         type: 'post',         url: "/product/UploadProductImage?fimgi=" + fimgi,         secureuri: false,         fileElementId: 'fimg' + fimgi,         dataType: "json",         success: function (data) {             alert("上傳成功!");             //alert(data.O);.ajaxFileUpload({         type: 'post',         url: "/product/UploadProductImage?fimgi=" + fimgi,         secureuri: false,         fileElementId: 'fimg' + fimgi,         dataType: "json",         success: function (data) {             alert("上傳成功!");             //alert(data.O);("#Img" + fimgi).val(data.O);         },         error: function (XMLHttpRequest, textStatus, e) {             alert(textStatus);             alert(e);         }     }); }

背景改為範圍ContentResult,且,ContentType = "text/html"。

public ContentResult UploadProductImage(int fimgi) {     HttpPostedFileBase head = this.Request.Files["fimg"+fimgi];     if (head == null)     {         return new ContentResult         {             Content = new JavaScriptSerializer { MaxJsonLength = int.MaxValue }.Serialize(new R() { F = 1, M = "對不起,無上傳内容!" }),             ContentType = "text/html"         };     var supportedTypes = new[] { "jpg", "jpeg", "png", "gif", "bmp" };     var fileExt = System.IO.Path.GetExtension(head.FileName).Substring(1);     if (!supportedTypes.Contains(fileExt))             Content = new JavaScriptSerializer { MaxJsonLength = int.MaxValue }.Serialize(new R() { F = 1, M = "對不起,隻能上傳 jpg, jpeg, png, gif, bmp!" }),     if (head.ContentLength > 1024 * 1024)             Content = new JavaScriptSerializer { MaxJsonLength = int.MaxValue }.Serialize(new R() { F = 1, M = "對不起,大小超出限制1M!" }),     var r = new Random();     var filename = Guid.NewGuid().ToString("N") + "." + fileExt;     string path = this.Server.MapPath("~/upload/product");     if (!Directory.Exists(path))         Directory.CreateDirectory(path);     var filepath = Path.Combine(path, filename);     head.SaveAs(filepath);     string webPath = "/upload/product/" + filename;     return new ContentResult         Content = new JavaScriptSerializer { MaxJsonLength = int.MaxValue }.Serialize(new R()             F = 0,             M = "上傳成功,儲存為:" + webPath + "!",             O = webPath         }),         ContentType = "text/html"     }; 本文轉自最課程陸敏技部落格園部落格,原文連結:http://www.cnblogs.com/luminji/p/4677103.html,如需轉載請自行聯系原作者
上一篇: 心靜思