C#fileName显示空格有问题

时间:2012-09-08 03:59:23

标签: c# file file-io

假设我的报告名称为:

string reportName = "Facebook Network Performance Summary Report (By Region, By Content)";

我希望将该文件名显示为excel

以下是导出文件的代码

Response.AddHeader("Content-Disposition", "attachment; filename=" reportName + ".xls");
Response.AddHeader("Content-Length", l.ToString());
Response.ContentType = "application/vnd.ms-excel";
Response.BinaryWrite(f);

不幸的是,当我阅读reportName的第一个空格时,它只显示Facebook而没有其他内容。

是否有任何用于ASP.NET或其他方法的wexisting API来处理fileNAme的空间?

5 个答案:

答案 0 :(得分:6)

尝试用双引号包装文件名。

 string filename = reportName + ".xls";
 Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");

答案 1 :(得分:3)

您需要将文件名包装在引号中:

Response.AddHeader("Content-Disposition", "attachment; filename=\"" + reportName + "\".xls");

答案 2 :(得分:2)

如何使用逐字字符串

string  reportName = @"Facebook Network Performance Summary Report (By Region, By Content)";

答案 3 :(得分:1)

尝试在双引号之前添加@和/或双引号以使c#读取文字

Problem with spaces in a filepath - commandline execution in C#

答案 4 :(得分:0)

尝试一下...

var fileName = HttpUtility.UrlEncode(document.FileName,Encoding.UTF8);