:sparkles:新增文件下载接口
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user