新增文件下载接口

This commit is contained in:
不做码农 2023-12-16 16:19:27 +08:00
parent 7c78ce8ed5
commit 0ee1ab8af5
4 changed files with 56 additions and 24 deletions

View File

@ -8,6 +8,7 @@ using Newtonsoft.Json.Serialization;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading.Tasks;
using System.Web; using System.Web;
namespace Infrastructure.Controllers namespace Infrastructure.Controllers
@ -75,6 +76,23 @@ namespace Infrastructure.Controllers
return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", HttpUtility.UrlEncode(fileName)); return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", HttpUtility.UrlEncode(fileName));
} }
/// <summary>
/// 下载文件
/// </summary>
/// <param name="path"></param>
/// <param name="fileName">文件名,一定要带扩展名</param>
/// <returns></returns>
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 #region
/// <summary> /// <summary>

View File

@ -144,7 +144,7 @@ namespace Infrastructure
/// 写文件 /// 写文件
/// </summary> /// </summary>
/// <param name="path">完整路径带扩展名的</param> /// <param name="path">完整路径带扩展名的</param>
/// <param name="content"></param> /// <param name="content">写入文件内容</param>
public static void WriteAndSave(string path, string content) public static void WriteAndSave(string path, string content)
{ {
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
@ -158,17 +158,7 @@ namespace Infrastructure
Console.WriteLine("开始写入文件Path=" + path); Console.WriteLine("开始写入文件Path=" + path);
try try
{ {
//实例化一个文件流--->与写入文件相关联 File.WriteAllText(path, content);
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();
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -151,6 +151,36 @@ namespace ZR.Admin.WebApi.Controllers
#endregion #endregion
/// <summary>
/// 下载文件
/// </summary>
/// <param name="path"></param>
/// <param name="fileId"></param>
/// <returns></returns>
[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);
}
/// <summary> /// <summary>
/// 初始化种子数据 /// 初始化种子数据
/// </summary> /// </summary>

View File

@ -55,13 +55,15 @@ namespace ZR.ServiceCore.Services
await formFile.CopyToAsync(stream); await formFile.CopyToAsync(stream);
} }
string uploadUrl = OptionsSetting.Upload.UploadUrl; 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) SysFile file = new(formFile.FileName, fileName, fileExt, fileSize + "kb", filePath, userName)
{ {
StoreType = (int)StoreType.LOCAL, StoreType = (int)StoreType.LOCAL,
FileType = formFile.ContentType, FileType = formFile.ContentType,
FileUrl = finalFilePath.Replace("\\", "/"), FileUrl = finalFilePath.Replace("\\", "/"),
AccessUrl = accessPath AccessUrl = fullUrl.AbsoluteUri
}; };
file.Id = await InsertFile(file); file.Id = await InsertFile(file);
return file; return file;
@ -111,22 +113,14 @@ namespace ZR.ServiceCore.Services
{ {
if (string.IsNullOrEmpty(str)) 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("-", ""); return BitConverter.ToString(MD5.HashData(Encoding.Default.GetBytes(str)), 4, 8).Replace("-", "");
} }
public Task<long> InsertFile(SysFile file) public Task<long> InsertFile(SysFile file)
{ {
try return Insertable(file).ExecuteReturnSnowflakeIdAsync();//单条插入返回雪花ID;
{
return Insertable(file).ExecuteReturnSnowflakeIdAsync();//单条插入返回雪花ID;
}
catch (Exception ex)
{
Console.WriteLine("存储图片失败" + ex.Message);
throw new Exception(ex.Message);
}
} }
} }
} }