Miamian
Miamian

Reputation: 217

Downloading pictures/Word documents using ASP.NET

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?

Upvotes: 0

Views: 923

Answers (2)

Miamian
Miamian

Reputation: 217

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

Upvotes: 0

o.k.w
o.k.w

Reputation: 25810

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.

Upvotes: 1

Related Questions