Sna*_*yes 5 c# asp.net header asp.net-mvc-3
我今天遇到了一个问题,对我来说很奇怪,但也许不是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 );
}
...
}
Run Code Online (Sandbox Code Playgroud)
如果提到并存储在path/ filePathvariable中的文件名包含空格
PR SimpleTest.xls
然后下载框包含文件名,就像PR没有任何额外的.

如果该文件名没有空格(如PR_SimpleTest.xls),则标题随附,PR_SimpleTest.xls我可以下载(显示完整的文件名及其扩展名).
在文件名包含空格的情况下,有解决问题的解决方案吗?
Dam*_*ver 13
Google搜索http标题空间会找到此知识库文章,该文章建议使用引号括起文件名.例如
HttpContext.Current.Response.AddHeader(
"Content-Disposition",
"attachment; filename=\"" + Path.GetFileName( filePath ) + "\"");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4646 次 |
| 最近记录: |