Avatar of kperelman
kperelman
 asked on

vb.net download file

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?
Visual Basic.NET

Avatar of undefined
Last Comment
kperelman

8/22/2022 - Mon
Nasir Razzaq

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

Because of Response.End()

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

ASKER
Actually I just wanted the code to stop.  I set a break point at the stop function but it never gets there?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Nasir Razzaq

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

ASKER
I have more code below but it never reaches it.
Nasir Razzaq

Show us the full code.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
kperelman

ASKER
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
Nasir Razzaq

Have you enabled debugging in web.config?
kperelman

ASKER
Yes.

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Rick

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

kperelman

ASKER
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.
Nasir Razzaq

2nd file in the same code? That can not be seen in the full code that you posted.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Rick

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

Rick

I meant,

   For i as Integer = 0 to n
Rick

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

Your help has saved me hundreds of hours of internet surfing.
fblack61
kperelman

ASKER
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()
SOLUTION
Nasir Razzaq

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Rick

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()
ASKER CERTIFIED SOLUTION
Rick

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
kperelman

ASKER
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.  

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
kperelman

ASKER
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
kperelman

ASKER
So does this code work for you?