Link to home
Start Free TrialLog in
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?
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Because of Response.End()

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

ASKER

Actually I just wanted the code to stop.  I set a break point at the stop function but it never gets there?
Why stop? It will stop any way at the End Sub. Or do you want to Pause? Why?
I have more code below but it never reaches it.
Show us the full code.
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
Have you enabled debugging in web.config?
Yes.

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

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

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.
2nd file in the same code? That can not be seen in the full code that you posted.
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

I meant,

   For i as Integer = 0 to n
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

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
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.  

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
So does this code work for you?