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

时间:2017-04-09 15:09:58

标签: c# asp.net httprequest httpresponse html-encode

我正在尝试使用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

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

1 个答案:

答案 0 :(得分:0)

例如,你可以使用:

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