我今天遇到了一个问题,这对我来说很奇怪,但也许对 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
的文件名,没有任何其他内容。
如果该文件名有 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/