ASP.net 使用 HttpContext.Current.Response 文件名下载文件



我正在尝试使用以下代码使用 System.Web.HttpContext.Current.Response 下载文件:

HttpResponse objResponse = System.Web.HttpContext.Current.Response;

我使用以下方法对文件名进行编码:

FileName = Uri.EscapeDataString(FileName);

文件正在正确下载,除非文件名中有逗号或点。

在这种情况下,在下载文件时,资源管理器无法解码逗号。

例如:

Original file name: Testing, File
Encoded name: Testing%2C%20file
Downloaded file name:  Testing%2C file

有没有办法对文件名进行编码/编码,以保留逗号和点?

例如,您可以使用:

public ActionResult DownloadFileXML(string filePath)
{
string fileName = Path.GetFileName(filePath);
return File(filePath, "application/xml", fileName);
}

最新更新