diff --git a/Infrastructure/Controllers/BaseController.cs b/Infrastructure/Controllers/BaseController.cs index d84a7d3..9a9f842 100644 --- a/Infrastructure/Controllers/BaseController.cs +++ b/Infrastructure/Controllers/BaseController.cs @@ -8,6 +8,7 @@ using Newtonsoft.Json.Serialization; using System; using System.Collections.Generic; using System.IO; +using System.Threading.Tasks; using System.Web; namespace Infrastructure.Controllers @@ -75,6 +76,23 @@ namespace Infrastructure.Controllers return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", HttpUtility.UrlEncode(fileName)); } + /// + /// 下载文件 + /// + /// + /// 文件名,一定要带扩展名 + /// + protected IActionResult DownFile(string path, string fileName) + { + if (!System.IO.File.Exists(path)) + { + return NotFound(); + } + var stream = System.IO.File.OpenRead(path); //创建文件流 + Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition"); + return File(stream, "application/octet-stream", HttpUtility.UrlEncode(fileName)); + } + #region 方法 /// diff --git a/Infrastructure/Helper/FileUtil.cs b/Infrastructure/Helper/FileUtil.cs index c6d6c7b..3b5de9b 100644 --- a/Infrastructure/Helper/FileUtil.cs +++ b/Infrastructure/Helper/FileUtil.cs @@ -144,7 +144,7 @@ namespace Infrastructure /// 写文件 /// /// 完整路径带扩展名的 - /// + /// 写入文件内容 public static void WriteAndSave(string path, string content) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) @@ -158,17 +158,7 @@ namespace Infrastructure Console.WriteLine("开始写入文件,Path=" + path); try { - //实例化一个文件流--->与写入文件相关联 - using var fs = new FileStream(path, FileMode.Create, FileAccess.Write); - //实例化一个StreamWriter-->与fs相关联 - using var sw = new StreamWriter(fs); - //开始写入 - sw.Write(content); - //清空缓冲区 - sw.Flush(); - //关闭流 - sw.Close(); - fs.Close(); + File.WriteAllText(path, content); } catch (Exception ex) { diff --git a/ZR.Admin.WebApi/Controllers/CommonController.cs b/ZR.Admin.WebApi/Controllers/CommonController.cs index c2c53a6..ef2d2f4 100644 --- a/ZR.Admin.WebApi/Controllers/CommonController.cs +++ b/ZR.Admin.WebApi/Controllers/CommonController.cs @@ -151,6 +151,36 @@ namespace ZR.Admin.WebApi.Controllers #endregion + /// + /// 下载文件 + /// + /// + /// + /// + [HttpGet] + [Verify] + [ActionPermissionFilter(Permission = "common")] + [Log(Title = "下载文件", IsSaveResponseData = false)] + public IActionResult DownloadFile(string? path, long fileId = 0) + { + var tempPath = path; + if (fileId > 0) + { + var fileInfo = SysFileService.GetById(fileId); + if (fileInfo != null) + { + tempPath = fileInfo.FileUrl; + } + } + string fullPath = tempPath; + if (tempPath.StartsWith("/")) + { + fullPath = Path.Combine(WebHostEnvironment.WebRootPath, tempPath.ReplaceFirst("/", "")); + } + string fileName = Path.GetFileName(fullPath); + return DownFile(fullPath, fileName); + } + /// /// 初始化种子数据 /// diff --git a/ZR.ServiceCore/Services/SysFileService.cs b/ZR.ServiceCore/Services/SysFileService.cs index deb4167..4be6d13 100644 --- a/ZR.ServiceCore/Services/SysFileService.cs +++ b/ZR.ServiceCore/Services/SysFileService.cs @@ -55,13 +55,15 @@ namespace ZR.ServiceCore.Services await formFile.CopyToAsync(stream); } string uploadUrl = OptionsSetting.Upload.UploadUrl; - string accessPath = string.Concat(uploadUrl, "/", filePath.Replace("\\", "/"), "/", fileName); + string accessPath = string.Concat(filePath.Replace("\\", "/"), "/", fileName); + Uri baseUri = new(uploadUrl); + Uri fullUrl = new(baseUri, accessPath); SysFile file = new(formFile.FileName, fileName, fileExt, fileSize + "kb", filePath, userName) { StoreType = (int)StoreType.LOCAL, FileType = formFile.ContentType, FileUrl = finalFilePath.Replace("\\", "/"), - AccessUrl = accessPath + AccessUrl = fullUrl.AbsoluteUri }; file.Id = await InsertFile(file); return file; @@ -111,22 +113,14 @@ namespace ZR.ServiceCore.Services { if (string.IsNullOrEmpty(str)) { - str = Guid.NewGuid().ToString(); + str = Guid.NewGuid().ToString().ToLower(); } return BitConverter.ToString(MD5.HashData(Encoding.Default.GetBytes(str)), 4, 8).Replace("-", ""); } public Task InsertFile(SysFile file) { - try - { - return Insertable(file).ExecuteReturnSnowflakeIdAsync();//单条插入返回雪花ID; - } - catch (Exception ex) - { - Console.WriteLine("存储图片失败" + ex.Message); - throw new Exception(ex.Message); - } + return Insertable(file).ExecuteReturnSnowflakeIdAsync();//单条插入返回雪花ID; } } }