• 上传文件


     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         }
  • 相关阅读:
    安装go 环境
    reading notes —— effective Java;目录结构,便于复习与查找
    reading notes -- Amazon.com Recommendations: Item-to-Item Collaborative Filtering
    reading notes -- 知识图谱简介
    reading notes -- A Report from the Trenches
    原生 js 简单实现 Promise
    JS 中屏幕、浏览器和文档的高度、宽度和距离
    添加jQuery方法解析url查询部分
    表单验证常用正则表达式
    HTML5 + CSS3 实现地球绕太阳公转
  • 原文地址:https://www.cnblogs.com/lyboke/p/14837855.html
Copyright © 2020-2023  润新知