ARTICLE

How to Download any file to the Browser in ASP.NET using VB.NET

Posted by Shalini Juneja Articles | ASP.NET using VB.NET April 15, 2011
In this article you will learn how you can download any type of file to the Browser.
Download Files:
 
Reader Level:

Introduction
 
Following article is simply showing that how you can Download any type of file to the Browser. In which using Streams you can provide a file to the user for without the need for FTP or any interference of the Internet Information Server (IIS), when you will create your Web application you have also add a folder to your application that include the file you want to download.

Getting Started

  • First you simply create a new ASP.NET web application. 
  • Now right click on your project in solution explorer and click add new folder. A folder will be added to your projectgive name images to the folder. Now any file that you want to download copy and paste in this folder as I have added a file named (StateManagement.ppt). 
  • Specify the folder name and file name in the code's link tag. 
  • Now write the below code in Default.aspx.

    <a href="images/StateManagement.ppt">Download the file</a>
    <%@ Page language="vb" runat="server" explicit="true" strict="true" %>
    <script language="vb" runat="server">
    Sub Page_Load(Sender As Object, E As EventArgs)
           Dim strRequest As String = Request.QueryString("file")
           If strRequest <> "" Then
                  Dim path As String = Server.MapPath(strRequest)
                  Dim file As System.IO.FileInfo = New System.IO.FileInfo(path)
                  If file.Exists Then
                         Response.Clear()
                         Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name)
                         Response.AddHeader("Content-Length", file.Length.ToString())
                         Response.ContentType = "application/octet-stream"
                         Response.WriteFile(file.FullName)
                         Response.End
                  Else
                         Response.Write("This file does not exist.")
                  End If
           Else
                Response.Write("Click link to download.")
           End
    If
    End Sub
    </script>
     
  • The Design mode of the Default.aspx page will look like below.

    File-Download.gif
     
  • Now run your application.

Output:-

File-Download2.gif

File-Download3.gif

File-Download4.gif

File-Download5.gif

File-Download6.gif

File-Download7.gif

Summary


In this article you learned how to Download any type of file to the Browser.

Login to add your contents and source code to this article
share this article :
post comment
 

Good Work.

Posted by Rohatash Kumar Apr 15, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor