封装
public ActionResult UploadPost(HttpPostedFileBase fileData, string rootPath, string rtnView)
{
string rootFolder = Server.MapPath("~/" + rootPath);
string fileName = "";
if (fileData != null)
{
try
{
if (!Directory.Exists(rootFolder))
{
Directory.CreateDirectory(rootFolder);
}
fileName = Guid.NewGuid().ToString() + Path.GetExtension(fileData.FileName);
string fullPathName = Path.Combine(rootFolder, fileName);
fileData.SaveAs(fullPathName);
var jsonResult = new
{
Msg = "上传成功",
fileName = rootPath + fileName
};
return Json(jsonResult);
}
catch (Exception ex)
{
var jsonResult = new
{
Msg = "上传失败",
Error = ex.ToString()
};
return Json(jsonResult);
}
}
return View(rtnView);
}
调用
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UploadPost(HttpPostedFileBase fileData)
{
return base.UploadPost(fileData, "UserUploadImges/ServicePhotographyMgr/", "ServiceTypeEdit");
}
[AcceptVerbs(HttpVerbs.Post)]不可在BaseContrBaseController加上,否则前台直接500 error