public async Task <IActionResult> UploadImage(IFormFile formFile, CancellationToken cancellationToken) { var param = new SingleImageUploadParam() { Request = Request, FormFile = formFile, RootPath = Web.WebRootPath, Module = "Test", Group = "Logo", ThumbCutMode = ThumbnailMode.Cut, Thumbs = new List <string> { "200x300", "400x200" }, }; var result = await _fileUploadService.UploadImageAsync(param, cancellationToken); return(View(result)); }
public async Task <Result <ImageFileInfo> > UploadImage([Required] IFormFile formFile, CancellationToken cancellationToken) { var param = new SingleImageUploadParam() { Request = Web.Request, FormFile = formFile, RootPath = Web.WebRootPath, Module = "upload", Group = "image", //*******裁剪和等比缩放 二者取其一******* //是否开自动裁剪原图(根据自定大小,自动对宽高裁剪) //IsCutOriginal = true, //AutoCutSize = 800, //是否开启等比缩放原图(根据等比大小和压缩质量裁剪) IsZoomOriginal = true, Ratio = 50, Quality = 100, //ThumbCutMode = ThumbnailMode.Cut, //Thumbs = new List<string> { "200x300", "400x200" }, }; var result = await _fileUploadService.UploadImageAsync(param, cancellationToken); result.Url = $"https://www.xx.com/{result.FullPath.Replace("\\", "/")}"; // oss 单文件上传 //var client = _ossFactory.GetClient("images"); //(var res1, string message) = client.Delete("upload/images/master/2019/11/28/test111111.png"); //(var res, string url) = client.Upload("upload/images/master/2019/11/28/test111111.png", @"C:\Users\Nigel\Downloads\QQ图片20200611104303.png"); return(RestFull.Success(data: result)); }
/// <summary> /// 上传图片。单张图片 /// </summary> /// <param name="param">参数</param> /// <param name="cancellationToken">取消令牌</param> public async Task <ImageFileInfo> UploadImageAsync(SingleImageUploadParam param, CancellationToken cancellationToken = default(CancellationToken)) { if (param.FormFile == null || param.FormFile.Length < 1) { if (param.Request.Form.Files != null && param.Request.Form.Files.Any()) { param.FormFile = param.Request.Form.Files[0]; } } if (param.FormFile == null || param.FormFile.Length < 1) { throw new ArgumentNullException("请选择文件!"); } string fileExt = FileHelper.GetExtension(param.FormFile.FileName); if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(param.Extensions, fileExt.ToLower()) == -1) { throw new Exception("上传图片扩展名是不允许的扩展名。只允许" + param.Extensions.Join(",") + "格式。"); } if (param.FormFile.Length > param.Size) { throw new Exception($"上传文件大小超过限制。图片大小不能超过{new FileSize(param.Size, FileSizeUnit.M).ToString()}。"); } var imageInfo = await SaveImageAsync(param.FormFile, param.RelativePath, param.RootPath, cancellationToken); if (param.IsCutOriginal) { string orgin = Path.Combine(param.RootPath, imageInfo.FullPath); string thumPath = $"{orgin}-tmp"; ImageHelper.MakeThumbnail(orgin, thumPath, param.OriginalWidth, param.OriginalHeight, ThumbnailMode.Cut); System.IO.File.Copy(thumPath, orgin, true); FileHelper.Delete(thumPath); } if (param.Thumbs.Count == 0 || param.Thumbs == null || param.Thumbs.Count == 0) { return(imageInfo); } foreach (var thumbSize in param.Thumbs) { var _size = thumbSize.Split('x', StringSplitOptions.RemoveEmptyEntries); if (_size.Length < 2) { continue; } var thumbPath = Path.Combine(imageInfo.Path, $"{imageInfo.Id.ToString().Replace("-", "")}-{thumbSize}.{imageInfo.Extension}"); var thumb = Path.Combine(param.RootPath, thumbPath); var orgin = Path.Combine(param.RootPath, imageInfo.FullPath); ImageHelper.MakeThumbnail(orgin, thumb, _size[0].ToInt(), _size[1].ToInt(), param.ThumbCutMode); imageInfo.Thumbs.TryAdd(thumbSize, thumbPath); } return(imageInfo); }