接收移动端上传的图片,示例代码:
[HttpPost]
[ApiSecurityFilter]
public IHttpActionResult UploadImg()
{
string imgs = "";
try
{
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
foreach (string key in files.AllKeys)
{
System.Web.HttpPostedFile file = files[key];
if (!String.IsNullOrEmpty(file.FileName))
{
string filePath = "/Upload/Images/" +DateTime.Now.ToString("yyyyMM") + "/";
string dir = System.Web.HttpContext.Current.Server.MapPath(filePath);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
string oldName = file.FileName;
string ext = oldName.Substring(oldName.LastIndexOf("."));
string newName = DateTime.Now.ToString("ddHHmmssfff") + ext;
string url = filePath + newName;
file.SaveAs(dir + newName);
imgs += "网址" + url + ",";
}
} //添加、更新到数据库 }
catch (Exception ex)
{
Log.Error("=========上传图片错误:" + ex.Message + "=============");
return Json(new { Result = "fail", Response = ex.Message });
}
return Json(new { Result = "ok", Response = imgs.TrimEnd(',') });
}