Search code examples
c#asp.netashx

ASHX Image Download on Click


I am using ashx to serve images from a database, is there anyway to have a user click on a link that allows them to download the file on the computer. (IE it shows the Save Dialog) Like you download a file. Is this possible to do?


Solution

  • If you want it to prompt to save make sure you add the following line when creating the response:

    context.Response.AppendHeader("Content-Disposition",
        "attachment;filename=" + filename);
    

    This will make the browser treat it like an attachment and prompt with the save dialog.

    EDIT: Based on your comment make sure you are building your response correctly:

    // set attachment header like above
    // then you need to get your file in byte[] form
    byte[] dataYouWantToServeUp = GetData();
    // you can set content type as well
    yourHttpContext.Response.ContentType = "image/jpg";
    // serve up the response
    yourHttpContext.Response.BinaryWrite(dataYouWantToServeUp);