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.
}
});