Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > ASP.Net > Download button


Reply
 
Thread Tools Display Modes
  #1  
Old 04-14-2012, 07:52 AM
Alex30 Alex30 is offline
Regular
 
Join Date: Sep 2007
Posts: 51
Default Download button


Hi,
In my project, I've created a download button.

This button shall perform 3tasks:
1. Change label text
2. Download file
3. Make the button invisible

The following code changes the label text and make the button invisible:
Code:
Protected Sub btnDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDownload.Click
              
        lblDownload.Text = "<h3>Thank you for downloading </h3>" 
        btnDownload.Visible = False

    End Sub
When I add a piece of code that handles the file download, the label text is not changed and button remains visible.
Here is the code:

Code:
Protected Sub btnDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDownload.Click
        Dim URL As String = "~/myfile.exe"
        Dim fileInfo As FileInfo = New FileInfo(Server.MapPath(URL))


        If fileInfo.Exists Then
            Response.Clear()
            Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name)
            Response.AddHeader("Content-Length", fileInfo.Length.ToString())
            Response.ContentType = "application/octet-stream"
            Response.Flush()
            Response.WriteFile(fileInfo.FullName)
        End If

        lblDownload.Text = "<h3>Thank you for downloading</h3>"
        btnDownload.Visible = False

    End Sub
File downloading cancels other operations.

Do you have any ideas?
Reply With Quote
  #2  
Old 04-16-2012, 10:38 AM
DrPunk's Avatar
DrPunk DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Change the text and hide the button before doing the file download...

Code:
Protected Sub btnDownload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDownload.Click
        Dim URL As String = "~/myfile.exe"
        Dim fileInfo As FileInfo = New FileInfo(Server.MapPath(URL))

        If fileInfo.Exists Then

            lblDownload.Text = "<h3>Thank you for downloading</h3>"
            btnDownload.Visible = False

            Response.Clear()
            Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name)
            Response.AddHeader("Content-Length", fileInfo.Length.ToString())
            Response.ContentType = "application/octet-stream"
            Response.Flush()
            Response.WriteFile(fileInfo.FullName)
        End If

    End Sub
Your code waits on Response.Writefile until it has finished, so therefore wasn't doing the other lines until it had finished.
__________________
There are no computers in heaven!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->