File Upload control in ASP.NET using VB.NET

In this article we demonstrate on what is FileUpload control and how you can upload a file using this control.
  • 11489

ASP.NET introduces a new FileUpload server control that makes the process of uploading a file to a server even simpler. When giving a page the Capability to upload files, you simply include the <asp:FileUpload> control. With the help of this control accepting file uploads from users has become extremely easy. In the previous version of ASP.NET there was a HTML control that was rendered as an input control of type file and you also need to set the forms ectype to multipart/form data to get the uploaded file on the server side code. But the new FileUpload control does almost all the work of setting the form's ectype etc. You can just drag and drop the control from designer and use any other control. Like a button control to do a post back and on the server side code get the file using the control's posted file property.
How to use FileUpload

  • Simply just drag a FileUpload control and a button on  page. The page will look like below.

  • You can also add controls by below code.

    <%@ Page Title="Home Page" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"
        CodeBehind="Default.aspx.vb" Inherits="File_Upload._Default" %>
    <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>Select a file to upload</h2>
    <asp:FileUpload ID="FileUpload1" Width="400px" runat="server" Font-Names="Verdana" Font-Size="11px" /><br />
    <asp:Button ID="cmdUpload" runat="server" Text="Upload File" Font-Names="Verdana" Font-Size="11px" OnClick="cmdUpload_Click" />
    <br />
    <asp:Literal ID="ltOne" runat="server" /> 
  • Then add the below code in code behind file of the page.

    Sub cmdUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            If Me.FileUpload1.HasFile Then
                Me.ltOne.Text = "File Uploaded Successfully"
            End If
        End Sub





More Articles

© 2020 DotNetHeaven. All rights reserved.