Failed to open save download dialog

Using the code below, I cannot show the open / save dialog as a file:

public void ProcessRequest(HttpContext context) { string link = context.Request.QueryString["Link"]; string extension = Path.GetExtension(link); string fileName = Path.GetFileName(link); string fullPath = String.Format("{0}\\{1}", context.Server.MapPath("~/Content/Uploads/"), fileName); if (File.Exists(fullPath)) { context.Response.ClearContent(); context.Response.ClearHeaders(); context.Response.AddHeader( "Content-Length", new FileInfo(fullPath).Length.ToString()); string contentType; switch (extension) { default: contentType = "application/octet-stream"; break; } context.Response.ContentType = contentType; context.Response.AddHeader( "Content-Disposition", String.Format("attachment; filename={0}", fileName)); context.Response.WriteFile(fullPath, true); context.Response.Flush(); } } 

I tried to close the answer, leave the answer open, use TrasmitFile() , but I never get any dialogue or any feedback. I also tried debugging it, but no exceptions arise. Tried in IE 7/8 and Chrome. Any help is appreciated.

Thank!

The following is the output of Fiddler:

HTTP / 1.1 200 OK Cache-Control: private Content-Length: 3813 Content-Type: application / octet-stream Server: Microsoft-IIS / 7.5 Content-Disposition: affiliation; file name = b1af9b34-28cc-4479-a056-8c55b41a5ece.txt X-AspNet version: 4.0.30319 X-Powered-By: ASP.NET Date: Thu, 23 December 2010 21:51:58 GMT

 * Home * Hotels * Reviews * Community * Travel Guide * Travel Insurance * Contact us 

* FIDDLER: RawDisplay is truncated with 128 characters. Right-click to disable truncation. *

+2
jquery c # iis-6
Dec 23 '10 at 21:25
source share
2 answers

Finally figured it out. Actually there is no problem with the code I posted. As you can see in Fiddler's output, the contents of the text file were successfully written to the response stream, and the headers used were also correct. The real problem is how the actual HTTP request was made. I used

$ get (urlToGenericHandler).

using jQuery. The reason why I cannot upload a file using AJAX or a callback model is beyond the scope of this answer. See jQuery Supported Data Types here

In any case, I changed the call using AJAX to use basic post-back.

Thanks to everyone that helped.

+2
Dec 23 '10 at 23:34
source share

Try to change

 contentType = "application/octet-stream"; 

to

 contentType = "application/download"; 

Update : Try replacing the title position and content type.

 context.Response.AddHeader( "Content-Disposition", String.Format("attachment; filename={0}", fileName)); context.Response.ContentType = contentType; context.Response.AddHeader( "Content-Length", new FileInfo(fullPath).Length.ToString()); 
+2
Dec 23 '10 at 22:13
source share



All Articles