zoukankan      html  css  js  c++  java
  • 上传文件

     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         }
  • 相关阅读:
    uboot nand erase 的显示错误修复
    Sougo for linux install.
    S3C6410移植uboot2010.3(2)基本的启动信息修改
    S3C6410移植uboot2010.3(4)uboot的dnw功能添加
    S3C6410移植uboot2010.3(3)正常化配置
    ubuntu乱码修复
    应老婆点(20070705 13:11:34)(新浪)
    克己慎独 2008923 13:32:00 (21ic)
    信任(20061229 14:16:32)(新浪)
    不要轻易承诺 2008926 14:42:00 (21ic)
  • 原文地址:https://www.cnblogs.com/lyboke/p/14837855.html
Copyright © 2011-2022 走看看