mvc上传图片
[HttpPost]
public JsonResult Upload()
{
if (Request.Files.Count > )
{
if (Request.Files.Count == )
{
HttpPostedFileBase file = Request.Files[];
if (file.ContentLength > )
{
string title = string.Empty;
title = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + Path.GetFileName(file.FileName);
string path = "../UploadFile/" + title;
path = System.Web.HttpContext.Current.Server.MapPath(path);
file.SaveAs(path);
return Json(new { status = true, url = path });
}
}
else
{
string[] urllist = new string[Request.Files.Count];
for (int i = ; i < Request.Files.Count; i++)
{
HttpPostedFileBase file = Request.Files[i];
if (file.ContentLength > )
{
string title = string.Empty;
title = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + Path.GetFileName(file.FileName);
string path = "../UploadFile/" + title;
path = System.Web.HttpContext.Current.Server.MapPath(path);
file.SaveAs(path);
urllist[i] = path;
}
}
return Json(new { status = true, url = urllist });
} }
else
{
return Json(new { status = false, url = "",msg="没有文件" });
} return Json(new { status = false, url = "",msg=""});
}
详情请看:https://www.cnblogs.com/jingch/p/5036686.html
webapi上传图片请看:https://www.cnblogs.com/webapi/p/10542077.html