5

I have an ashx file with an IHttpHandler. When I POST some data to this IHttpHandler, I do some work, create a file, and then want that file returned to the user so that they may save the file via their browser.

With the file created, this is how I attempt to write the file back to the response:

HttpResponse response = context.Response;
response.ClearContent();
response.Clear();
response.ContentType = "application/octet-stream";
response.AddHeader("Content-Disposition", "attachment; filename=MYFILE.EXT");
response.WriteFile("C:\tempstuff\MYFILE.EXT");

In a finally block later on, I will call:

response.End()

When I call this Handler, nothing happens. Response 200 is returned, no errors are thrown - but the browser will not prompt the user to save this file.

Here is what the response, as captured by Fiddler, looks like:

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Thu, 23 Aug 2012 12:12:19 GMT
X-AspNet-Version: 4.0.30319
Content-Disposition: attachment; filename=MYFILE.EXT
Cache-Control: private
Content-Type: application/octet-stream
Content-Length: 781053
Connection: Close

[raw content of the file here]

This response looks correct to me. It contains the content of the file - however under any of the major browsers, no file dialog prompts me to save the file.

What am I doing wrong here?

Update: In case it helps, here is where I am using JavaScript from my web application to call this HttpHandler.

$.ajax({
    type: 'POST',
       url: ashxUrl,
       data: postData,
       success: function (result) {
           // Doin' stuff on success
       },
       error: function (error) {
           // Doin' stuff on error.
        }
});
3
  • I tried the exact same thing. I made a simple page Download.ashx with the code above, and POSTed to it from another page, Default.aspx. In all three browsers (IE, FF, Chrome), it works perfectly, and I am getting almost the exact same headers in Fiddler. Not sure what else could be going on? Commented Aug 23, 2012 at 13:17
  • Interesting! Maybe it would help to describe my JavaScript where I am explicitly making this request call. I will edit the main post to include this snippet. EDIT: I added the JS snippet. Commented Aug 23, 2012 at 13:19
  • 6
    Ah. Yes that does help. You can't do a file download via AJAX. You'll need an alternate solution like this. Commented Aug 23, 2012 at 13:22

1 Answer 1

3

You've said that you want the results of the web-call to be made available to your javascript. It would be extremely annoying if that caused the browser to prompt you to save it, as then all AJAX would be impossible.

You want the browser itself to make the call. For a GET that would be a simple matter of setting self.location.href. For a POST the easiest way is normally to fill out a hidden form with the data, and then submit() it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.