上传文件

 1        
 2   public readonly IWebHostEnvironment _webHostEnvironment;
 3 
 4         public FileController(IWebHostEnvironment webHostEnvironment)
 5         {
 6             
 7             _webHostEnvironment = webHostEnvironment;
 8         }
 9  /// <summary>
10         /// 上传文件
11         /// </summary>
12         /// <param name="files"></param>
13         /// <returns></returns>
14         
15         public ActionResult Uploads()
16         {
17             List<TmpUrl> list = new List<TmpUrl>();
18             var files = Request.Form.Files;
19             //var a = Guid.NewGuid();
20             Random random = new Random();
21             var randomNumber = random.Next();
22             string webRootPath = _webHostEnvironment.WebRootPath;
23             var fileName = randomNumber + "-old.jpg";
24             foreach (var formFile in files)
25             {
26                 if (formFile.Length > 0)
27                 {
28                     var filePath = Path.Combine(webRootPath + "\\images", fileName);
29                     using (var stream = System.IO.File.Create(filePath))
30                     {
31                         formFile.CopyTo(stream);
32                         TmpUrl tu = new TmpUrl();
33                         tu.Url = @"/images/" + fileName;
34                         tu.Name = fileName;
35                         list.Add(tu);
36 
37                         //生成缩略图:得到指定像素缩略图,图片较小
38                         //long quality = 75;
39                         //var thumbnailName = randomNumber + "-11.jpg";
40                         //var thumbnailPath = Path.Combine(webRootPath + "\\images" , thumbnailName);
41                         //var originalImage = System.Drawing.Image
42                         //    .FromStream(stream);
43                         //ImagesHelper.GetSmallImage(originalImage, thumbnailPath, 230, 160, quality, "CUT");
44 
45                         //生成缩略图:生成等比例高清缩略图,图片较大
46                         var thumbnailName = randomNumber + "-22.jpg";
47                         var thumbnailPath = Path.Combine(webRootPath + "\\images", thumbnailName);
48                         var originalImage = System.Drawing.Image
49                             .FromStream(stream);
50                         ImagesHelper.GetThumbnailImage(originalImage, 230, 160).Save(thumbnailPath);
51                     }
52                 }
53             }
54             return Json(list);
55         }
56 
57         /// <summary>
58         /// 图片上传本地服务器
59         /// </summary>
60         /// <param name="environment"></param>
61         /// <returns></returns>
62         [HttpPost]
63         [ValidateAntiForgeryToken]
64         [Obsolete]
65         public async Task<IActionResult> OnPostUpload([FromServices] IHostingEnvironment environment)
66         {
67             List<TmpUrl> list = new List<TmpUrl>();
68             var files = Request.Form.Files;
69             string webRootPath = environment.WebRootPath;
70             string contentRootPath = environment.ContentRootPath;
71             foreach (var formFile in files)
72             {
73                 if (formFile.Length > 0)
74                 {
75                     var fileName = Guid.NewGuid().ToString() + "-old.jpg";
76                     var path = Path.Combine(environment.WebRootPath + "\\images\\upload", fileName);
77                     using (var fs = new FileStream(path, FileMode.CreateNew))
78                     {
79                         await formFile.CopyToAsync(fs);
80                         TmpUrl tu = new TmpUrl();
81                         tu.Url = @"/images/upload/" + fileName;
82                         list.Add(tu);
83 
84                         //生成缩略图1:生成等比例高清缩略图,图片比较大
85                         var thumbnailName = Guid.NewGuid().ToString() + "-11.jpg";
86                         var thumbnailPath = Path.Combine(environment.WebRootPath + "\\images\\upload", thumbnailName);
87                         var originalImage = System.Drawing.Image.FromStream(fs);
88                         ImagesHelper.GetThumbnailImage(originalImage, 230, 160).Save(thumbnailPath);
89 
90                     }
91                 }
92             }
93 
94             return new JsonResult(list);
95         }

 

posted @ 2021-06-01 16:42  Ly博客  阅读(45)  评论(0编辑  收藏  举报