✨新增文件下载接口
This commit is contained in:
parent
7c78ce8ed5
commit
0ee1ab8af5
@ -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));
|
||||
}
|
||||
|
||||
/// <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 方法
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -144,7 +144,7 @@ namespace Infrastructure
|
||||
/// 写文件
|
||||
/// </summary>
|
||||
/// <param name="path">完整路径带扩展名的</param>
|
||||
/// <param name="content"></param>
|
||||
/// <param name="content">写入文件内容</param>
|
||||
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)
|
||||
{
|
||||
|
||||
@ -151,6 +151,36 @@ namespace ZR.Admin.WebApi.Controllers
|
||||
|
||||
#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>
|
||||
|
||||
@ -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<long> InsertFile(SysFile file)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Insertable(file).ExecuteReturnSnowflakeIdAsync();//单条插入返回雪花ID;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("存储图片失败" + ex.Message);
|
||||
throw new Exception(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user