//Web或WebAPI獲取表單資料流(批量檔案上傳)
public JsonResult UploadFile()
{
//HttpPostedFileBase fileBase = Request.Files["fileToUploadKeyID"];
HttpPostedFileBase fileBase = Request.Files[0]; //獲取客戶端上載的檔案的集合
string resultUrl = string.Empty;//相對檔案路徑
string errMsg = string.Empty;
if (fileBase == null || fileBase.ContentLength == 0)
{
errMsg = "檔案為空";
}
else
{
int MaxSize = 1024 * 1024 * 4;
if (fileBase.InputStream.Length > MaxSize)
{
errMsg = "檔案過大";
}
else
{
try
{
//回圈遍歷批量上傳的檔案
for (int i = 0; i < Request.Files.Count; i++)
{
fileBase = Request.Files[i];
var Name = System.IO.Path.GetFileName(fileBase.FileName);
var fileName = "/upload/" + DateTime.Now.ToString("yyMMddHHmmssffff") + "." + Name.Split('.')[1];
var filePath = System.Web.HttpContext.Current.Server.MapPath(fileName);
fileBase.SaveAs(filePath);//保存檔案
resultUrl += fileName + ";";//拼接檔案相對路徑
}
}
catch
{
errMsg = "上傳失敗";
}
}
}
return Json(new { errMsg = errMsg, resultUrl = resultUrl.Trim(';') });
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/12315.html
標籤:ASP.NET
上一篇:layui + mvc + ajax 匯出Excel功能
下一篇:解決連接oracle報錯 嘗試加載Oracle客戶端庫時引發BadImageFomatException。如果在安裝64位Oracle客戶端組件的情況下以32位模式運行,將出現此問題的報錯。
