We help IT Professionals succeed at work.

vb.net download file

kperelman
kperelman asked
on
384 Views
Last Modified: 2012-09-12
I have this code behind for my asp.net page:

Protected Sub Download_Button_Click(sender As Object, e As System.EventArgs) Handles Download_Button.Click

 Response.ContentType = "image/jpeg"
 Response.AppendHeader("Content-Disposition", "attachment; filename=boat.jpg")
 Response.TransmitFile(Server.MapPath("~/images/boat.jpg"))
 Response.End()
 Stop
End Sub

The file gets downloaded but why does Stop not get reached?
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
What do you want the Stop to do? Is that a keyword(never seen) or a function call?

Commented:
Because of Response.End()

I don't think you need the 'Stop' anyway.

Author

Commented:
Actually I just wanted the code to stop.  I set a break point at the stop function but it never gets there?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Why stop? It will stop any way at the End Sub. Or do you want to Pause? Why?

Author

Commented:
I have more code below but it never reaches it.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Show us the full code.

Author

Commented:
I have a site master with a default page with a button and this is the default page code behind.

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click

        Dim temp_value As Integer = 0

        Response.ContentType = "image/jpeg"
        Response.AppendHeader("Content-Disposition", "attachment; filename=boat.jpg")
        Response.TransmitFile(Server.MapPath("~/images/boat.jpg"))
        Response.End()

        temp_value = temp_value + 1   '<-- this line has a breakpoint but the debugger does not stop here

    End Sub
End Class
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Have you enabled debugging in web.config?

Author

Commented:
Yes.

I can breakpoint at the line: Response.End()

Commented:
Have you tried this?

With Response

       .ContentType = "image/jpeg"
       .AppendHeader("Content-Disposition", "attachment; filename=boat.jpg")
       .TransmitFile(Server.MapPath("~/images/boat.jpg"))

        temp_value = temp_value + 1

       .End()

End

Author

Commented:
rick_gwu:

Sorry but because I need to donwload a 2nd file  in the vb after the

        temp_value = temp_value + 1

 your work around will not be a good solution.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
2nd file in the same code? That can not be seen in the full code that you posted.

Commented:
I wasn't aware that you needed to download a second file... please explain what your requirements are in more detail.

If you're going to download n number of files, you could try something like this:
For i As Integer = 1 To n

   With Response
      .ContentType = "image/" & imgType
      .AppendHeader("Content-Disposition", "attachment; filename=" & img)
      .TransmitFile(Server.MapPath("~/images/" & img))
      .Clear()
   End With

   temp_value = temp_value + 1

Next

Response.End()

Open in new window

Commented:
I meant,

   For i as Integer = 0 to n

Commented:
If you only have 2 files, try this:
With Response

   .ContentType = "image/jpeg"
   .AppendHeader("Content-Disposition", "attachment; filename=boat.jpg")
   .TransmitFile(Server.MapPath("~/images/boat.jpg"))
   .ClearHeaders()

   temp_value += 1

   .AppendHeader("Content-Disposition", "attachment; filename=jetski.jpg")
   .TransmitFile(Server.MapPath("~/images/jetski.jpg"))

   temp_value += 1

   .End()

End With

Open in new window

Author

Commented:
I tried this.  I get prompted and can download boat1 but I am not prompted for boat2?

        Response.ContentType = "image/jpeg"
        Response.AppendHeader("Content-Disposition", "attachment; filename=boat.jpg")
        Response.TransmitFile(Server.MapPath("~/images/boat.jpg"))
        Response.Clear()
        Response.AppendHeader("Content-Disposition", "attachment; filename=boat2.jpg")
        Response.TransmitFile(Server.MapPath("~/images/boat.jpg"))
        Response.End()
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
Try .ClearHeaders()


Response.ContentType = "image/jpeg"
Response.AppendHeader("Content-Disposition", "attachment; filename=boat.jpg")
Response.TransmitFile(Server.MapPath("~/images/boat.jpg"))
Response.ClearHeaders()
Response.AppendHeader("Content-Disposition", "attachment; filename=boat2.jpg")
Response.TransmitFile(Server.MapPath("~/images/boat.jpg"))
Response.End()
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
I copied your clearheaders() code but it did not work. Only the second file was created.

The code to download 2 files with a button click is what you have.  

Author

Commented:
Here is all of the code:

site master page:
<%@ Master Language="VB" AutoEventWireup="false" CodeFile="Site.Master.vb" Inherits="Site" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
    <title></title>
</head>
<body>
    <form runat="server">
        <div class="main">
            <asp:ContentPlaceHolder ID="MainContent" runat="server"/>
        </div>
     </form>
</body>
</html>

default page:
<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false"
    CodeFile="Default.aspx.vb" Inherits="_Default" %>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Button ID="Button1" runat="server" Text="Button" />
</asp:Content>

default page code behind:

Imports System.IO
Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        Response.ContentType = "image/jpeg"
        Response.AppendHeader("Content-Disposition", "attachment; filename=boat.jpg")
        Response.TransmitFile(Server.MapPath("~/images/boat.jpg"))
        Response.ClearHeaders()
        Response.AppendHeader("Content-Disposition", "attachment; filename=boat2.jpg")
        Response.TransmitFile(Server.MapPath("~/images/boat2.jpg"))
        Response.End()
    End Sub
End Class

Author

Commented:
So does this code work for you?
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.