public ActionResult Upload()
{
string imgurl = "";
foreach (string key in Request.Files)
{
HttpPostedFileBase file0 = Request.Files[key];
Stream stream;
int size = file0.ContentLength / 1024;
if (size > 1024)
{
return Content(ReturnMsg(Enum_Return.失败, "图片不能超过1M:", null));
}
byte[] fileByte = new byte[2];
stream = file0.InputStream;
stream.Read(fileByte, 0, 2);
string fileFlag = "";
if (fileByte != null && fileByte.Length > 0)
{
fileFlag = fileByte[0].ToString() fileByte[1].ToString();
}
string[] fileTypeStr = { "255216", "7173", "6677", "13780" };
if (fileTypeStr.Contains(fileFlag))
{
string action = Request["action"];
string path = "/uploads/";
switch (action)
{
case "headimage":
path = "headimage/";
break;
case "blogtype":
path = "blogtype/";
break;
}
string fullpath = path UserInfo.userID "/";
if (!Directory.Exists(Server.MapPath(fullpath)))
{
Directory.CreateDirectory(Server.MapPath(fullpath));
}
Request.Files[key].SaveAs(Server.MapPath(fullpath Request.Files[key].FileName));
imgurl = fullpath Request.Files[key].FileName;
}
else
{
return Content(ReturnMsg(Enum_Return.失败, "图片格式不正确:" fileFlag, null));
}
stream.Close();
}
return Content(ReturnMsg(Enum_Return.成功, "上传成功", imgurl));
}