【发布时间】:2019-04-29 05:53:23
【问题描述】:
我正在尝试下载某些文件中包含非 ascii 字符的文件,此时下载失败。它给出了以下错误。
标头中的非 ASCII 或控制字符无效:0x000D
该代码适用于 ascii 字符。我将响应标头中的文件名传递给以 Angular 6 开发的 UI。
下面是我的代码
[HttpGet]
[Route("someroute/downloadfile")]
public IActionResult DownloadFile(int ID)
{
try
{
using (ExcelPackage pck = bal.DownloadFile(ID))
{
System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
{
FileName = pck.File.Name,
};
Response.Headers.Add("Content-Disposition", cd.ToString());
return File(pck.GetAsByteArray(), "application /vndopenxmlformats-officedocument.spreadsheetml.sheet");
}
}
catch (Exception ex)
{
throw ex;
}
}
但是,我的要求是我想按原样下载文件,就好像它有非 ascii 字符一样。
对此的任何帮助表示赞赏。
【问题讨论】:
-
带有 0x000D 的文件名不是有效的文件名。解决这个问题
-
你尝试过编码吗?
-
@CalinVlasin 不。这就是我要找的东西。你能帮我吗?
-
@Tseng 由于文件名包含非 ascii 字符而产生的异常。我想用非 ascii 字符下载
-
请参考:blog.softartisans.com/2013/06/03/…。 stackoverflow上有很多关于这个的参考。
标签: c# asp.net-web-api asp.net-core asp.net-core-2.0