/// <summary>
/// App上传图片
/// </summary>
/// <returns>返回上传图片的相对路径</returns>
[HttpPost]
public AppReturn<string> UploadImage()
{
AppReturn<string> rModel = new AppReturn<string>();
//string result = "";
// 检查是否是 multipart/form-data
if (!Request.Content.IsMimeMultipartContent("form-data"))
{
InvestmentCommon.Log4NetHelper.Log.Error("不是有效的'form-data'类型");
rModel.state = 0;
rModel.msg = "不是有效的'form-data'类型";
return rModel;
}
DateTime dt = DateTime.Now;
string path = string.Format("/imagestore/{0}/{1}{2}", dt.Year, dt.Month.ToString().PadLeft(2, '0'), dt.Day.ToString().PadLeft(2, '0'));
string abtPath = HttpContext.Current.Server.MapPath(path);
if (!InvestmentCommon.FileHelper.CreateDirectory(abtPath))
{
InvestmentCommon.Log4NetHelper.Log.Error(string.Format("创建目录{0}失败", abtPath));
rModel.state = 0;
rModel.msg = "创建图片目录失败";
return rModel;
}
string fileName = "";
string ext = "";
string filePath = "";
try
{
HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];//获取传统context
HttpRequestBase request = context.Request;//定义传统request对象
HttpFileCollectionBase imgFiles = request.Files;
for (int i = 0; i < imgFiles.Count; i++)
{
ext = InvestmentCommon.FileHelper.GetExtention(imgFiles[i].FileName);
fileName = string.Format("{0}{1}", System.Guid.NewGuid().ToString(), ext);
filePath = string.Format("{0}/{1}", path, fileName);
imgFiles[i].SaveAs(abtPath + "\\" + fileName);
imgFiles[i].InputStream.Position = 0;
rModel.data = filePath.Replace("/", "");
rModel.state = 1;
rModel.msg = "success";
}
}
catch (Exception e)
{
InvestmentCommon.Log4NetHelper.Log.Error("图片保存失败");
rModel.state = 0;
rModel.msg = "图片保存失败";
return rModel;
}
//result = Newtonsoft.Json.JsonConvert.SerializeObject(rList);
return rModel;
}
/// <summary>
/// App上传图片
/// </summary>
/// <returns>返回上传图片的相对路径</returns>
[HttpPost]
public AppReturn<string> UploadImageByBase64(FileUp file)
{
AppReturn<string> rModel = new AppReturn<string>();
if (string.IsNullOrEmpty(file.FileBase64))
{
InvestmentCommon.Log4NetHelper.Log.Error("没有选择要上传的图片");
rModel.state = 0;
rModel.msg = "没有选择要上传的图片";
return rModel;
}
DateTime dt = DateTime.Now;
string path = string.Format("/imagestore/{0}/{1}{2}", dt.Year, dt.Month.ToString().PadLeft(2, '0'), dt.Day.ToString().PadLeft(2, '0'));
string abtPath = HttpContext.Current.Server.MapPath(path);
if (!InvestmentCommon.FileHelper.CreateDirectory(abtPath))
{
InvestmentCommon.Log4NetHelper.Log.Error(string.Format("创建目录{0}失败", abtPath));
rModel.state = 0;
rModel.msg = "创建图片目录失败";
return rModel;
}
string fileName = "";
string filePath = "";
try
{
byte[] imgByte = Convert.FromBase64String(file.FileBase64);
MemoryStream ms = new MemoryStream(imgByte);
Image image = System.Drawing.Image.FromStream(ms);
fileName = string.Format("{0}.png", System.Guid.NewGuid().ToString());
filePath = string.Format("{0}/{1}", path, fileName);
image.Save(abtPath + "\\" + fileName);
rModel.data = filePath.Replace("/", "");
rModel.state = 1;
rModel.msg = "success";
ms.Close();
ms.Dispose();
}
catch (Exception e)
{
InvestmentCommon.Log4NetHelper.Log.Error("图片保存失败");
rModel.state = 0;
rModel.msg = "图片保存失败";
return rModel;
}
return rModel;
}