#region 上传产品图片
/// <summary>
/// 上传产品图片 多文件上传图片
/// </summary>
/// <returns></returns>
// GET api/Admin/UpLoadProductImg/
[Route("api/Admin/UpLoadProductImg/")]
[HttpPost]
public async Task<ProductImgResult> UpLoadProductImg()
{
string imgurl = string.Empty;
ProductImgModel model = new ProductImgModel();
try
{
var content = Request.Content;
var uploadDir = System.Web.HttpContext.Current.Server.MapPath("~/DownloadFile/");

if (content.Headers.ContentLength > 0)
{
var sp = new MultipartMemoryStreamProvider();
Task.Run(async () => await Request.Content.ReadAsMultipartAsync(sp)).Wait();
HttpContent item = sp.Contents.LastOrDefault();
string remark = string.Empty;
string ProductImgType = string.Empty;
if (sp.Contents.Count > 1)
{
remark = sp.Contents[0].ReadAsStringAsync().Result;
ProductImgType = sp.Contents[1].ReadAsStringAsync().Result;
}
if (item != null)
{
if (item.Headers.ContentDisposition.FileName != null)
{
var ms = item.ReadAsStreamAsync().Result;
//字节转kb
long kb = ms.Length / 1024;
Stream stream = await content.ReadAsStreamAsync();
Image iSource = Image.FromStream(ms);
string url = "";
var file_name = item.Headers.ContentDisposition.FileName;
//FileInfo fileinfo = new FileInfo(file_name);
string[] strArray = file_name.Split('.');
string Suffix = "." + strArray[1].Replace("\"", "");
var filename = Guid.NewGuid().ToString("N");
var uploadfile = filename + Suffix;

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

if (ProductImgType == "1")//缩略图
{
if (iSource.Width != 180 || iSource.Height != 180 || kb > 120)
{
//压缩图片
Tools t = new Tools();
url = t.CompressSizePictures(iSource, ProductImgType, kb, uploadDir, filename, Suffix);
}
else
{
//ms = item.ReadAsStreamAsync().Result;
using (var br = new BinaryReader(ms))
{
string path = uploadDir + uploadfile;
var data = br.ReadBytes((int)ms.Length);
File.WriteAllBytes(path, data);
}
url = "https://" + Request.RequestUri.Host + "/DownloadFile/" + uploadfile;
}
}
else if (ProductImgType == "2")//产品详情滚动图片
{
if (iSource.Width != 750 || iSource.Height != 564 || kb > 120)
{
//压缩图片
Tools t = new Tools();
url = t.CompressSizePictures(iSource, ProductImgType,kb, uploadDir, filename, Suffix);
}
else
{
//var ms = item.ReadAsStreamAsync().Result;
using (var br = new BinaryReader(ms))
{
string path = uploadDir + uploadfile;
var data = br.ReadBytes((int)ms.Length);
File.WriteAllBytes(path, data);
}
url = "https://" + Request.RequestUri.Host + "/DownloadFile/" + uploadfile;
}
}
else if (ProductImgType == "3")//产品详情图片
{

}
else
{
//var ms = item.ReadAsStreamAsync().Result;
using (var br = new BinaryReader(ms))
{
string path = uploadDir + uploadfile;
var data = br.ReadBytes((int)ms.Length);
File.WriteAllBytes(path, data);
}
url = "https://" + Request.RequestUri.Host + "/DownloadFile/" + uploadfile;
}
try
{
productimage imgmodel = new productimage();
imgmodel.productid = string.Empty;
imgmodel.productimageurl = url;
imgmodel.productimageid = filename;
imgmodel.productimagename = uploadfile;
imgurl = url;
imgmodel.remark = remark;
imgmodel.productimagetype = ProductImgType;
context.productimage.Add(imgmodel);
context.SaveChanges();
}
catch (Exception ex)
{

}
model.productimageid = filename;
model.remark = remark;
model.productimgtype = ProductImgType;
}
}

}
}
catch (Exception ex)
{
return new ProductImgResult { ResCode = ResCode.Fail, ResMsg = ex.ToString() };
}
model.imgurl = imgurl;
return new ProductImgResult { ResCode = ResCode.Ok, model = model, ResMsg = "添加数据成功" };
}
#endregion 上传产品图片

 

 

 

 

 

/// <summary>
/// 图片压缩尺寸和质量
/// </summary>
/// <param name="iSource">图片</param>
/// <param name="ProductImgType">类型</param>
/// <param name="filesize">实际文件大小</param>
/// <param name="uploadDir">文件路径</param>
/// <param name="filename">文件名</param>
/// <param name="Suffix">后缀名</param>
/// <returns></returns>
public string CompressSizePictures(Image iSource, string ProductImgType, long filesize, string uploadDir, string filename, string Suffix)
{
long[] qy = new long[1];
int width = 0, height = 0, maxsize = 0;
if (ProductImgType == "1")
{
//设置指定宽高
maxsize = 40;
width = 180;
height = 180;
qy[0] = (maxsize * 100 / Convert.ToInt32(filesize));//压缩比例
}
if (ProductImgType == "2")
{
//设置指定宽高
maxsize = 120;
width = 750;
height = 564;
qy[0] = (maxsize * 100 / Convert.ToInt32(filesize));
}
ImageFormat tFormat = iSource.RawFormat;
int sW = iSource.Width, sH = iSource.Height;//图片尺寸宽高
Bitmap filemap = new Bitmap(width, height);
Graphics g = Graphics.FromImage(filemap);
g.Clear(Color.WhiteSmoke);
g.CompositingQuality = CompositingQuality.HighQuality;
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(iSource, new Rectangle((640 - sW) / 2, (360 - sH) / 2, sW, sH), 0, 0, iSource.Width, iSource.Height, GraphicsUnit.Pixel);
g.Dispose();
EncoderParameters ep = new EncoderParameters();
EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
ep.Param[0] = eParam;
ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo jpegICIinfo = null;
for (int x = 0; x < arrayICI.Length; x++)
{
if (arrayICI[x].FormatDescription.Equals("JPEG"))
{
jpegICIinfo = arrayICI[x];
break;
}
}
filemap.Save(uploadDir + filename + Suffix, jpegICIinfo, ep);//dFile是压缩后的新路径
filemap.Dispose();
return uploadDir + filename + Suffix;
}

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄