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:
Using
Response.TransmitFile(FileName)
insteadLeaving out
Response.End()
Leaving out the
Content-Length
headerUsing
this.ControllerContext.HttpContext.Response
Any help would be greatly appreciated
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.Not sure if it's your only problem, but the file name in the
Content-Disposition
header is supposed to be aquoted-string
, so you'll need to add quotes around it, particularly since it contains spaces;On a side note,
Response.End()
also flushes the buffer, so yourResponse.Flush()
is redundant.