File Uploading in ASP.NET using VB.NET

In this article, I am going to show you how you can upload a file through ASP.NET.
  • 2425

The File uploading has always been a very tedious task for the web developer community. Either we have to use a third party component or write numerous lines of code. But with ASP.NET, uploading files become extremely easy. Now it can be done with a small amount of code lines with the FileUpload control.

In this article, I am going to show you how you can upload a file through ASP.NET.

 

<%@ Page Language="vb" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<html>
<
head>
<
script language="VB" runat="server">
 
Sub Upload(Source As Object, e As EventArgs)
   If Not (myFile.PostedFile Is Nothing) Then
      Dim intFileNameLength as Integer
      Dim strFileNamePath as String
      Dim strFileNameOnly as String

      'Logic to find the FileName (excluding the path)
      strFileNamePath = MyFile.PostedFile.FileName
      intFileNameLength = Instr(1, StrReverse(strFileNamePath), "\")
      strFileNameOnly = Mid(strFileNamePath, (Len(strFileNamePath)-intFileNameLength)+2)
      myFile.PostedFile.SaveAs("c:\inetpub\wwwroot\yourwebapp\upload\" & strFileNameOnly)
      lblMsg.Text = "Uploading Complete."
   End If
 End Sub
</script>
 <style type="text/css">
    .style1
     {
        color: #003399;
        border-style: groove;
     }
 </style>
</head>
<
body>
<
title>ASP.NET Example</title>
 
<h3 style="font-family: Verdana; font-size: small; height: 25px; background-color: #DFEFFF;">
   &nbsp;<br />
   <span class="style1">Upload Files In ASP.NET&nbsp;&nbsp; 
   </span>
</
h3>
<form id="Form1" enctype="multipart/form-data" runat="server" 
   style="font-family: Verdana; font-size: small; background-color: #DFEFFF; height: 145px;">
   <br />
  Choose File: <input id="myFile" type="file" runat="server" 
   style="font-family: Verdana; font-size: small"><br />
   <br /><br />
   &nbsp;<asp:label id=lblMsg runat="server" />
  <input id="Button1" type=button value="Upload" OnServerClick="Upload" 
   runat="server" style="font-family: Verdana; font-size: small">
</form>
</
body>

When you run this application then the window will look like this:

fileupload 1.gif

Now click on the Browse button to select a file for uploading. After selecting the file, click on upload button and you will get the message after completion of file uploading.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.