From 582db250d5ae4ab90bd9d4b0e47b8839e4f11eac Mon Sep 17 00:00:00 2001 From: Jimmy <584481786@qq.com> Date: Fri, 22 Oct 2021 14:53:13 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=84=E4=BB=B6=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GalleryFiles/GalleryFilesAppService.cs | 50 ++++++++++--------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/Code/src/MineTec.ProManager.Application/GalleryFiles/GalleryFilesAppService.cs b/Code/src/MineTec.ProManager.Application/GalleryFiles/GalleryFilesAppService.cs index 1762be1..425d127 100644 --- a/Code/src/MineTec.ProManager.Application/GalleryFiles/GalleryFilesAppService.cs +++ b/Code/src/MineTec.ProManager.Application/GalleryFiles/GalleryFilesAppService.cs @@ -133,15 +133,16 @@ namespace MineTec.ProManager.GalleryFiles } //单个文件上传 - public async Task UploadFilessss(IFormFile file) + public async Task UploadFile(IFormFile file) { if (file == null) { return "请先选择文件"; } - string webRootPath = @"D:\UploadingFiles\"; // wwwroot 文件夹 - string uploadPath = Path.Combine("uploads", DateTime.Now.ToString("yyyyMMdd")); + string webRootPath = @"D:\UploadingFiles\"; //保存文件路径 + string uploadPath = Path.Combine("uploads", DateTime.Now.ToString("yyyyMMdd"));//增加日期文件夹 string dirPath = Path.Combine(webRootPath, uploadPath); + //创建路径 if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); @@ -159,33 +160,34 @@ namespace MineTec.ProManager.GalleryFiles } //多个文件上传 - public async Task UploadPhotoszzzzz(IFormFileCollection files) + public async Task UploadFiles(IFormFileCollection files) { - long size = files.Sum(f => f.Length); - - string webRootPath = @"D:\UploadingFiles\"; // wwwroot 文件夹 - string uploadPath = Path.Combine("uploads", DateTime.Now.ToString("yyyyMMdd")); - string fileFolder = Path.Combine(webRootPath, uploadPath); - - if (!Directory.Exists(fileFolder)) - Directory.CreateDirectory(fileFolder); - + if (files.Count() == 0) + { + return "请先选择文件"; + } + string webRootPath = @"D:\UploadingFiles\"; //保存文件路径 + string uploadPath = Path.Combine("uploads", DateTime.Now.ToString("yyyyMMdd"));//增加日期文件夹 + string dirPath = Path.Combine(webRootPath, uploadPath); + //创建路径 + if (!Directory.Exists(dirPath)) + { + Directory.CreateDirectory(dirPath); + } foreach (var file in files) { - if (file.Length > 0) - { - var fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + - Path.GetExtension(file.FileName); - var filePath = Path.Combine(fileFolder, fileName); - using (var stream = new FileStream(filePath, FileMode.Create)) - { - await file.CopyToAsync(stream); - } + + string fileExt = Path.GetExtension(file.FileName).Trim('.'); //文件扩展名,不含“.” + string newFileName = Guid.NewGuid().ToString().Replace("-", "") + "." + fileExt; //随机生成新的文件名 + var filePath = Path.Combine(dirPath, newFileName); + + using (var stream = new FileStream(filePath, FileMode.Create)) + { + await file.CopyToAsync(stream); } } - - var url = $@"\{uploadPath}\11"; + var url = $@"\{uploadPath}\{dirPath}"; return url; } }