Skip navigation

The requirement of uploading a file to the web server is a requirement, which is, required a lot of web applications. We will look at how to do the same in this code tip. ASP.NET makes it very easy to upload the required file to the web server. There are two pieces of code which are presented below

  • HTML code
  • Code Behind Code

 

Given below is the HTML Code.

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="Mytest.WebForm1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>

            <HEAD>

                        <title>WebForm1</title>

                        <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">

                        <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">

                        <meta content="JavaScript" name="vs_defaultClientScript">

                        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">

            </HEAD>

            <body MS_POSITIONING="GridLayout">

                        <form id="Form1" method="post" encType="multipart/form-data" runat="server">

                                    <INPUT id="File1" style="Z-INDEX: 101; LEFT: 327px; POSITION: absolute; TOP: 123px" type="file"

                                                name="File1" runat="server">

                                    <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 393px; POSITION: absolute; TOP: 173px" runat="server"

                                                Text="Button"></asp:Button>

                                    <asp:Label id="lblMsg" style="Z-INDEX: 103; LEFT: 378px; POSITION: absolute; TOP: 77px" runat="server"

                                                ForeColor="Red"></asp:Label></form>

            </body>

</HTML>

 

 

 

The Code Behind Code for the Button’s Click event is given below. It is pretty self explanatory.

 

 

If Not File1.PostedFile.FileName = "" Then

            Dim sFile As String = File1.PostedFile.FileName

            sFile = sFile.Substring(sFile.LastIndexOf("\")).Replace("\", "")

            Dim sPath As String = AppDomain.CurrentDomain.BaseDirectory & sFile

            File1.PostedFile.SaveAs(sPath)

            lblMsg.Text = "File Uploaded Successfully"

        Else

            lblMsg.Text = "Please Select a file to be uploaded"

        End If

 

Happy Coding !!!

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish