/// <summary> /// 批量上传商品详情图片 /// </summary> /// <returns></returns> [HttpPost] public ContentResult uploadproductdpicArray(string path) { rData<List<string>> result = new rData<List<string>>(); result = UpLoadPicArray(path); if (result.resultState == 1) for (int i = 0; i < result.Data.Count; i++) { if (ST.Tool.ImageHelp.GetImageSuffix(result.Data[i]) != ".gif") { string imgPath = Server.MapPath($"~{result.Data[i]}"); string imgPathNoSuffix = imgPath.Substring(0, imgPath.LastIndexOf(".")); string imgSuffix = ST.Tool.ImageHelp.GetImageSuffix(imgPath); Image oldimg = Image.FromFile(imgPath); //读取图片 //压缩宽度为500的图片,等比 清晰度 100 ST.Tool.ImageHelp.PicThumbnail(oldimg, imgPath + "_E500_100" + imgSuffix, 500, 0, 100); oldimg.Dispose(); result.Data[i] = result.Data[i] + "_E500_100" + imgSuffix; } } var jsonResult = JsonConvert.SerializeObject(result); return new ContentResult() { Content = jsonResult }; } /// <summary> /// 上传图片 /// </summary> /// <param>保存图片的文件夹名称</param> /// <returns>保存结果</returns> private rData<string> UpLoadPic(string _path="public") { rData<string> result = new rData<string>(); HttpFileCollectionBase _file = Request.Files; if (_file.Count > 0) { long size = _file[0].ContentLength; string type = _file[0].ContentType; string name = _file[0].FileName; //文件格式 string _tp = Path.GetExtension(name); if (_tp.ToLower() == ".jpg" || _tp.ToLower() == ".jpeg" || _tp.ToLower() == ".gif" || _tp.ToLower() == ".png" || _tp.ToLower() == ".swf") { Stream stream = _file[0].InputStream; Image image = Image.FromStream(stream); string dateDir = DateTime.Now.ToString("yyyyMMdd"); string saveName = ST.Tool.ExpandString.GetNonceNumberT(16) + _tp; string filePath = $"{BaseConfig.headpath}{_path}/{dateDir}/"; string path = Server.MapPath(filePath); if (!Directory.Exists(path)) Directory.CreateDirectory(path); //_file[0].SaveAs(Server.MapPath($"{filePath}{saveName}")); //初始化图片对象 //Image image = new Bitmap(Server.MapPath($"{filePath}{saveName}")); foreach (var p in image.PropertyItems) { if (p.Id == 0x112) { var rft = p.Value[0] == 6 ? RotateFlipType.Rotate90FlipNone : p.Value[0] == 3 ? RotateFlipType.Rotate180FlipNone : p.Value[0] == 8 ? RotateFlipType.Rotate270FlipNone : p.Value[0] == 1 ? RotateFlipType.RotateNoneFlipNone : RotateFlipType.RotateNoneFlipNone; p.Value[0] = 0; //旋转属性值设置为不旋转 image.SetPropertyItem(p); //回拷进图片流 image.RotateFlip(rft); } } //重新保存为正常的图片 image.Save(Server.MapPath($"{filePath}{saveName}")); result.Data = $"{filePath}{saveName}"; } else result.errorMsg = "只能上传图片。"; } else result.errorMsg = "未选择文件"; return result; } /// <summary> /// 上传多张图片 /// </summary> /// <param></param> /// <returns></returns> private rData<List<string>> UpLoadPicArray(string _path = "public") { rData<List<string>> result = new rData<List<string>>(); result.Data = new List<string>(); HttpFileCollectionBase _file = Request.Files; if (_file.Count > 0) for (int i = 0; i < _file.Count; i++) { //Thread.Sleep(500); long size = _file[i].ContentLength; string type = _file[i].ContentType; string name = _file[i].FileName; //文件格式 string _tp = Path.GetExtension(name); if (_tp.ToLower() == ".jpg" || _tp.ToLower() == ".jpeg" || _tp.ToLower() == ".gif" || _tp.ToLower() == ".png" || _tp.ToLower() == ".swf") { Stream stream = _file[i].InputStream; Image image = Image.FromStream(stream); string dateDir = DateTime.Now.ToString("yyyyMMdd"); string saveName = ST.Tool.ExpandString.GetNonceNumberT(16) + _tp; string filePath = $"{BaseConfig.headpath}{_path}/{dateDir}/"; string path = Server.MapPath(filePath); if (!Directory.Exists(path)) Directory.CreateDirectory(path); //_file[0].SaveAs(Server.MapPath($"{filePath}{saveName}")); //初始化图片对象 //Image image = new Bitmap(Server.MapPath($"{filePath}{saveName}")); foreach (var p in image.PropertyItems) { if (p.Id == 0x112) { var rft = p.Value[0] == 6 ? RotateFlipType.Rotate90FlipNone : p.Value[0] == 3 ? RotateFlipType.Rotate180FlipNone : p.Value[0] == 8 ? RotateFlipType.Rotate270FlipNone : p.Value[0] == 1 ? RotateFlipType.RotateNoneFlipNone : RotateFlipType.RotateNoneFlipNone; p.Value[0] = 0; //旋转属性值设置为不旋转 image.SetPropertyItem(p); //回拷进图片流 image.RotateFlip(rft); } } //重新保存为正常的图片 image.Save(Server.MapPath($"{filePath}{saveName}")); result.Data.Add($"{filePath}{saveName}"); //result.Data = $"{filePath}{saveName}"; } else result.errorMsg = "只能上传图片。"; } else result.errorMsg = "未选择文件"; return result; }
总结