0

I've looked at many resources and the following should work, however my save as dialog box is never showing (not browser specific):

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();

The file definitely exists. I've also tried the following:

  1. Using Response.TransmitFile(FileName) instead

  2. Leaving out Response.End()

  3. Leaving out the Content-Length header

  4. Using this.ControllerContext.HttpContext.Response

Any help would be greatly appreciated

Iswanto San
  • 18,263
  • 13
  • 58
  • 79
lohiaguitar91
  • 522
  • 5
  • 9
  • I have attempted to use the suggested answer here: http://stackoverflow.com/questions/8897458/asp-net-download-file-to-client-browser but this also did not work – lohiaguitar91 Mar 17 '13 at 07:32
  • Update: I did get an HTTP connection closed error on Response.End(), but cannot reproduce it. – lohiaguitar91 Mar 17 '13 at 21:34

2 Answers2

2

Not sure if it's your only problem, but the file name in the Content-Disposition header is supposed to be a quoted-string, so you'll need to add quotes around it, particularly since it contains spaces;

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

On a side note, Response.End() also flushes the buffer, so your Response.Flush() is redundant.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • Here's the file name: C:\Users\MyName\Documents\Project\MyTest\MyTest\researcherExport.doc which contains no spaces. – lohiaguitar91 Mar 17 '13 at 21:34
  • @lohiaguitar91 `string downloadName = "Request "+request.RequestID+ "_researchExport.doc";` would seem to build a file name `Request xx_researchExport.doc` with a space in it. – Joachim Isaksson Mar 17 '13 at 21:59
  • ah yes. I did remove the space to now read `"Request+request.RequestID+ "_researchExport.doc"` however still no download prompt. Checked many different resources and it seems like what I'm doing should work. Surrounding it in a try-catch block did not produce any exception. – lohiaguitar91 Mar 17 '13 at 22:20
0

Solved the issue. I was using Ajax to call this function from one of my views. This didn't work (I am not sure why), but I think it may be because I have multiple controllers. Calling this from @HTML.ActionLink() had the prompt display properly.

lohiaguitar91
  • 522
  • 5
  • 9