2013-03-17 49 views
0

我已经看了很多资源及以下应该工作,但是我的另存为对话框从不说明(不针对特定浏览器):Response.WriteFile() - 不工作的ASP净MVC 4.5

Response.ContentType = "application/octet-stream"; 
string downloadName = "Request "+request.RequestID+ "_researchExport.doc"; 
Response.AddHeader("Content-Length", 
    new System.IO.FileInfo(FileName).Length.ToString()); 
Response.AddHeader("Content-Disposition", 
    string.Format("attachment; filename={0};", downloadName)); 
Response.WriteFile(FileName); 
Response.Flush(); 
Response.End(); 

该文件肯定存在。我也试过如下:

  1. 使用Response.TransmitFile(FileName)代替

  2. 离开了Response.End()

  3. 离开了Content-Length

  4. 使用this.ControllerContext.HttpContext.Response

任何帮助将是大大赞赏

+0

我试图在这里使用建议的答案:http://stackoverflow.com/questions/8897458/asp-net-download-file-to-client-browser但这也没有工作 – lohiaguitar91 2013-03-17 07:32:46

+0

更新:我没有得到一个HTTP连接在Response.End()上关闭了错误,但无法重现它。 – lohiaguitar91 2013-03-17 21:34:49

回答

0

解决了这个问题。我正在使用Ajax从我的一个视图中调用此函数。这不起作用(我不知道为什么),但我认为这可能是因为我有多个控制器。从@HTML.ActionLink()调用此提示已正确显示提示。

2

不知道如果这是你唯一的问题,但在Content-Dispositionis supposed to be a quoted-string文件名,所以你需要添加引号它,特别是因为它包含空格;

Response.AddHeader("Content-Disposition", 
    string.Format("attachment; filename=\"{0}\"", downloadName)); 

在一个侧面说明,Response.End()也刷新缓冲区,所以你Response.Flush()是多余的。

+0

不幸的是,没有奏效。 – lohiaguitar91 2013-03-17 21:32:45

+0

下面是文件名:C:\ Users \ MyName \ Documents \ Project \ MyTest \ MyTest \ researcherExport.doc其中不包含空格。 – lohiaguitar91 2013-03-17 21:34:27

+0

@ lohiaguitar91'string downloadName =“Request”+ request.RequestID +“_researchExport.doc”;'似乎会建立一个名为'Request xx_researchExport.doc'的文件名,其中包含一个空格。 – 2013-03-17 21:59:19