C#fileName显示空格有问题

问题描述 投票:2回答:5

假设我有一个名为:

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的空间?

c# file file-io
5个回答
6
投票

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

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

3
投票

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

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

2
投票

如何使用逐字字符串

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

1
投票

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

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


0
投票

试试这个...

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

© www.soinside.com 2019 - 2024. All rights reserved.