#region 上传图片
[HttpPost]
/// <summary>
/// 上传文件 jpg图片
/// </summary>
/// <param name="imgStr">Base64位数据</param>
/// <returns>一个虚拟的地址</returns>
public string UpLoadImg(string imgStr)
{
string urlPath = string.Empty;
try
{
// 转化64位数据
Image img = this.ToImageByBase64(imgStr);
// 定义文件名称
string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg";
// 定义保存的文件夹
string bodyPath = "/UpLoadImg/";
// 设置B站点路径
string basePath = "E:\\webservice2"; ;
string savabodyPath = bodyPath.Replace(‘/‘, ‘\\‘);
string newPath = basePath + savabodyPath;
// 如果文件夹不存在则创建
if (Directory.Exists(newPath) == false)
{
Directory.CreateDirectory(newPath);
}
string savaPath = newPath + fileName;
// 将得到的Image对象画到新的画布上
Image newImg = new Bitmap(img.Width, img.Height);
Graphics draw = Graphics.FromImage(newImg);
draw.DrawImage(img, 0, 0);
img.Dispose(); // 释放原来的对象
//保存文件 并修改数据库地址 新的图片对象保存
newImg.Save(savaPath);
urlPath = bodyPath + fileName; // 构建返回值
}
catch (Exception)
{
urlPath = "";
}
//返回一个地址
return urlPath;
}
/// <summary>
/// 将Base64字符串转化为图片
/// </summary>
/// <param name="base64"></param>
/// <returns></returns>
private System.Drawing.Image ToImageByBase64(string base64)
{
byte[] arr = Convert.FromBase64String(base64);
MemoryStream ms = new MemoryStream(arr);
Image bmp = new Bitmap(ms);
ms.Close();
return bmp;
}
#endregion