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; } }