kperelman
asked on
vb.net download file
I have this code behind for my asp.net page:
Protected Sub Download_Button_Click(send er As Object, e As System.EventArgs) Handles Download_Button.Click
Response.ContentType = "image/jpeg"
Response.AppendHeader("Con tent-Dispo sition", "attachment; filename=boat.jpg")
Response.TransmitFile(Serv er.MapPath ("~/images /boat.jpg" ))
Response.End()
Stop
End Sub
The file gets downloaded but why does Stop not get reached?
Protected Sub Download_Button_Click(send
Response.ContentType = "image/jpeg"
Response.AppendHeader("Con
Response.TransmitFile(Serv
Response.End()
Stop
End Sub
The file gets downloaded but why does Stop not get reached?
What do you want the Stop to do? Is that a keyword(never seen) or a function call?
Because of Response.End()
I don't think you need the 'Stop' anyway.
I don't think you need the 'Stop' anyway.
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?
ASKER
I have more code below but it never reaches it.
Show us the full code.
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("Con tent-Dispo sition", "attachment; filename=boat.jpg")
Response.TransmitFile(Serv er.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
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("Con
Response.TransmitFile(Serv
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?
ASKER
Yes.
I can breakpoint at the line: Response.End()
I can breakpoint at the line: Response.End()
Have you tried this?
With Response
.ContentType = "image/jpeg"
.AppendHeader("Content-Dis position", "attachment; filename=boat.jpg")
.TransmitFile(Server.MapPa th("~/imag es/boat.jp g"))
temp_value = temp_value + 1
.End()
End
With Response
.ContentType = "image/jpeg"
.AppendHeader("Content-Dis
.TransmitFile(Server.MapPa
temp_value = temp_value + 1
.End()
End
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.
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:
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()
I meant,
For i as Integer = 0 to n
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
ASKER
I tried this. I get prompted and can download boat1 but I am not prompted for boat2?
Response.ContentType = "image/jpeg"
Response.AppendHeader("Con tent-Dispo sition", "attachment; filename=boat.jpg")
Response.TransmitFile(Serv er.MapPath ("~/images /boat.jpg" ))
Response.Clear()
Response.AppendHeader("Con tent-Dispo sition", "attachment; filename=boat2.jpg")
Response.TransmitFile(Serv er.MapPath ("~/images /boat.jpg" ))
Response.End()
Response.ContentType = "image/jpeg"
Response.AppendHeader("Con
Response.TransmitFile(Serv
Response.Clear()
Response.AppendHeader("Con
Response.TransmitFile(Serv
Response.End()
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try .ClearHeaders()
Response.ContentType = "image/jpeg"
Response.AppendHeader("Con tent-Dispo sition", "attachment; filename=boat.jpg")
Response.TransmitFile(Serv er.MapPath ("~/images /boat.jpg" ))
Response.ClearHeaders()
Response.AppendHeader("Con tent-Dispo sition", "attachment; filename=boat2.jpg")
Response.TransmitFile(Serv er.MapPath ("~/images /boat.jpg" ))
Response.End()
Response.ContentType = "image/jpeg"
Response.AppendHeader("Con
Response.TransmitFile(Serv
Response.ClearHeaders()
Response.AppendHeader("Con
Response.TransmitFile(Serv
Response.End()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
The code to download 2 files with a button click is what you have.
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.Mas ter" AutoEventWireup="false"
CodeFile="Default.aspx.vb" Inherits="_Default" %>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="Main Content">
<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("Con tent-Dispo sition", "attachment; filename=boat.jpg")
Response.TransmitFile(Serv er.MapPath ("~/images /boat.jpg" ))
Response.ClearHeaders()
Response.AppendHeader("Con tent-Dispo sition", "attachment; filename=boat2.jpg")
Response.TransmitFile(Serv er.MapPath ("~/images /boat2.jpg "))
Response.End()
End Sub
End Class
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.Mas
CodeFile="Default.aspx.vb"
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="Main
<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("Con
Response.TransmitFile(Serv
Response.ClearHeaders()
Response.AppendHeader("Con
Response.TransmitFile(Serv
Response.End()
End Sub
End Class
ASKER
So does this code work for you?