日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

ajaxFileUpload上传文件后提示下载的问题

發布時間:2023/12/15 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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))
??? {
??????? return new ContentResult
??????? {
??????????? Content = new JavaScriptSerializer { MaxJsonLength = int.MaxValue }.Serialize(new R() { F = 1, M = "對不起,只能上傳 jpg, jpeg, png, gif, bmp!" }),
??????????? ContentType = "text/html"
??????? };
??? }

??? if (head.ContentLength > 1024 * 1024)
??? {
??????? return new ContentResult
??????? {
??????????? Content = new JavaScriptSerializer { MaxJsonLength = int.MaxValue }.Serialize(new R() { F = 1, M = "對不起,大小超出限制1M!" }),
??????????? ContentType = "text/html"
??????? };
??? }

??? 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,如需轉載請自行聯系原作者

總結

以上是生活随笔為你收集整理的ajaxFileUpload上传文件后提示下载的问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。