Error Handling When Downloading File from ASP.NET Web Handler (.Ashx)

Error handling when downloading file from ASP.NET Web Handler (.ashx)

First of all on this answer of mine the user ask how to make the download with javascript. But if you have the link you do not necessary need to add it to javascript, you can add it also using a regular download link.

Now many web sites use a page with some text that says "and now your download will start in few seconds. If not start please click here bla bla bla". One of the reasons is similar to yours, the user must know that something will start the download in few seconds and if not, then the user understand that there is a problem with out other extra message.

In general when you going to send a file for download, you are not in the page any more, you can not send message in the middle of the download, - or when the file ends with pdf, then is probably not going to show anything on a web page. So from my point of view if you do have issues and throw exceptions, you need to design some steps on how you inform the user that something was going wrong - like the middle page that inform the user that "now you going to receive a file - if the file not start the download in few seconds please do that"

However, when there is an exception thrown in the handler they are
redirected to the URL of the handler and shown a blank page.

To avoid that black page, on error you can return that code and the user is stay on page as it is, but still did not get any error message.

Response.TrySkipIisCustomErrors = true;
Response.Status = "204 No Content";
Response.StatusCode = 204;
Response.End();

Alternative on exception you can try to redirect him to an error page using the

Response.Redirect("errorpage.aspx");

and maybe you can send and some error ids there.

You can also try to just show a simple message as

context.Response.ContentType = "text/html";
context.Response.Write("Error downloading file - Please contact with us");

file download by calling .ashx page

Your file is downloading, but you get it on javascript, on the data parameter of your call because you call it with Ajax.

You use a handler - so ajax not needed here, and the most easy thing to do using javascript is that:

window.location = "FileDownload.ashx?parametres=22";

or with a simple link as

  <a target="_blank" href="FileDownload.ashx?parametres=22" >download...</a>

Ah, and send the parameters via the url, you can not post them that way.

You can also read: What is the best way to download file from server

ASP.NET Download Handler Works in IE but not Chrome

Try removing "inline" from the Content-Disposition header as "inline" & "attachment" aren't supposed to be used together:

context.Response.AddHeader("Content-Disposition", "attachment; filename=\""
+ fileInfo.Name + "\"");

ASP.NET file download from server

You can use an HTTP Handler (.ashx) to download a file, like this:

DownloadFile.ashx:

public class DownloadFile : IHttpHandler 
{
public void ProcessRequest(HttpContext context)
{
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition",
"attachment; filename=" + fileName + ";");
response.TransmitFile(Server.MapPath("FileDownload.csv"));
response.Flush();
response.End();
}

public bool IsReusable
{
get
{
return false;
}
}
}

Then you can call the HTTP Handler from the button click event handler, like this:

Markup:

<asp:Button ID="btnDownload" runat="server" Text="Download File" 
OnClick="btnDownload_Click"/>

Code-Behind:

protected void btnDownload_Click(object sender, EventArgs e)
{
Response.Redirect("PathToHttpHandler/DownloadFile.ashx");
}

Passing a parameter to the HTTP Handler:

You can simply append a query string variable to the Response.Redirect(), like this:

Response.Redirect("PathToHttpHandler/DownloadFile.ashx?yourVariable=yourValue");

Then in the actual handler code you can use the Request object in the HttpContext to grab the query string variable value, like this:

System.Web.HttpRequest request = System.Web.HttpContext.Current.Request;
string yourVariableValue = request.QueryString["yourVariable"];

// Use the yourVariableValue here

Note - it is common to pass a filename as a query string parameter to suggest to the user what the file actually is, in which case they can override that name value with Save As...

Best way to handle a 404 in a file download HTTP handler?

Pawel, it seems you've answered your question already...

...(so that they can contact
support)...

By stating the above I would suggest you create a custom 404 page which notifies the user of the file not existing on disk and provide them information on how to get in contact with the support office.

I've created a HTTP Handler for handling files and if a file does not exist on disk then I return a 404 response. I've setup IIS to display a custom page if a 404 reponse has been thrown.. (and I do the same for error 500).

Hope this helps and good luck with finding the solution that fits your needs!



Related Topics



Leave a reply



Submit