ASP.NET Core 接口返回文件类型文件名中文乱码
一、问题
返回文件时给定的文件名包含中文
浏览器中实际的下载文件名
发现文件名中文没有被正确的处理
二、解决方案
返回文件类型时,文件名用Uri.EscapeDataString(fileName)
来进行URL编码。
C# 全选
/// <summary>
/// 下载报表模板
/// </summary>
/// <returns></returns>
[HttpGet]
public IActionResult Down(string id)
{
var (fileBytes, fileName) = _business.Down(id);
if (fileBytes == null || fileBytes.Length == 0)
{
return NotFound("文件不存在");
}
// 添加命名空间:Microsoft.AspNetCore.Http
Response.Headers.Append("Access-Control-Expose-Headers", "Content-Disposition");
return File(fileBytes, "application/octet-stream", Uri.EscapeDataString(fileName));
}
版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
post 张国生