c# - 如果在 ASP.NET MVC 3 中有空间,为什么文件名 header 不完整?

标签 c# asp.net asp.net-mvc-3 header

我今天遇到了一个问题,这对我来说很奇怪,但也许对 C# 领域的专家来说不是。

我有一个名为 Download 的函数(一段代码!)

public void Download (string path){
  HttpContext.Current.Response.ContentType = "application/octet-stream";

  try {
           ....//process a 'filePath' variable using the 'path' parameter

               using ( FileStream sourceFile = new FileStream( filePath, FileMode.Open ) ) {
                 ...

                  HttpContext.Current.Response.AddHeader( "Content-Disposition", "attachment; filename=" + Path.GetFileName( filePath ) );

                  HttpContext.Current.Response.AddHeader( "Content-Length", fileSize.ToString() );

                  HttpContext.Current.Response.BinaryWrite( getContent );
               }
           ...

}

如果提到并存储在path/filePath变量中的文件名包含像

这样的空格

PR SimpleTest.xls

然后下载框中包含类似 PR 的文件名,没有任何其他内容。

enter image description here

如果该文件名有 NO 空格(如 PR_SimpleTest.xls),则 header 带有 PR_SimpleTest.xls,我可以下载为这样(显示完整的文件名和他的扩展名)。

当文件名包含空格时,有解决方案吗?

最佳答案

Google 搜索 http headers spaces 发现这个 Knowledge Base article这建议用引号将文件名括起来。例如

HttpContext.Current.Response.AddHeader(
    "Content-Disposition",
    "attachment; filename=\"" + Path.GetFileName( filePath ) + "\"");

关于c# - 如果在 ASP.NET MVC 3 中有空间,为什么文件名 header 不完整?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12506899/

相关文章:

asp.net-mvc - 数据注释 - 如何在 MVC3 中用 Web.Config 值替换 Range 值?

jquery - 需要能够使用元素验证 div 的方法(使用不显眼的/jquery 验证插件),而无需查看任何表单

c# - Expression<Func<..>>.Compile() 的实际类型是什么?

ASP.NET MVC - jquery 日期选择器

c# - Telerik Radgrid : adding another footer row

asp.net - 从 "https://www"重定向到 "https://"

c# - 如何在 C# 中为位图设置宽度和高度?

c# - 如何使用格式剪切、复制和粘贴?

c# - 如何在 asp.net 中获取链接的 http 类型(http 或 https)的完整域?

c# - 使用 .Contains 检查字符串是否包含字符时出错