views:

83

answers:

2

If I put the following code:

    Response.ContentType = "image/jpeg"
    Response.AppendHeader("Content-Disposition", "attachment; filename=capitol.jpg")
    Response.WriteFile(MapPath("capitol.jpg"))

into Page_Load, I will get the dialog box to download the image. But when I put the same code into a sub routine:

Private Sub downloadPic()
    MsgBox("Hello!")
    Response.ContentType = "image/jpeg"
    Response.AppendHeader("Content-Disposition", "attachment; filename=capitol.jpg")
    Response.WriteFile(Server.MapPath("capitol.jpg"))
    Response.End()
End Sub

I get the MsgBox (just for testing) but I don't get the ability to download the image. Any ideas?

+1  A: 

You can't output to the page and also push the download content within a single request/response.

Anyway your code won't work properly with a Response.Clear() before assigning header and WriteFile.

o.k.w
Thanks for your response. When I comment out the Page_Load code and try only the code in the downloadPic() sub routine, it doesn't download. Do you know any good links to help me with this?
Miamian
A: 

Thank you for the comment. The problem with my code was that I had the WriteFile code within an UpdatePanel. That was my mistake!

Miamian