.net core部署linux下载文件 文件为中文下载

在项目中,需要下载模板,某个文件夹下有excel文件或者word等  文件名为中文的时候在linux服务器下载不了  

如果直接写路由的话在windows下是可以直接访问的,但是core部署在linux上 所以会有些不同

第一种:写一个接口使用流下载

[httpget("download")]

public async Task<FileResult>  DownLoad()

{

  var filePath = System.IO.Path.Path.Join(System.IO.Path.GetDirectoryName(typeof(Program).Assembly.Location),"UpLoad","文件名。xlsx");

         return File(System.IO.File.ReadAllBytes(filePath),"application/octet-stream;charset=utf-8",System.IO.Path.GetFileName(filePath));

return null;

}

 

第二种:使用中间件,写一个解码中间件,然后把文件路径给前端,拼接上端口使用 windos.open 可直接下载

首先先写一个中间件类

public class UrlDecodeMiddleware

{

  private readonly RequestDelegate _next;

      public UrlDecodeMiddleware(RequestDelegate)

      {

  _next=next;

      }

     public async Task Invoke(HttpContext context)

 {

  if(context.Request.Method="Get")

  {

      context.Request.QueryString= new QueryString(HttpUtility.UrlDecode(context.Request.QueryString.Value));

      }

  await _next(context);

    }

}

///拓展方法将中间件加入到请求处理通道中

public static class UrlDecodeMiddlewateExtensions

{

  public static IapplicationBuilder UserUrlDecode(this IApplicationBuilder app)

  {

    return app.UserMiddleware<UrlDecodeMiddleware>();

  }

}

最后在Startup中的Configure中使用

app.UseUrlDecode();  就可以了

posted @   Best丶zhaotf  阅读(43)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示