2013-03-17 69 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