ARTICLE

File Uploading in ASP.NET

Posted by Sapna Articles | ASP.NET using VB.NET February 03, 2011
In this article, I am going to show you how you can upload a file through ASP.NET.
 
Reader Level:

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.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.
 

Login to add your contents and source code to this article
share this article :
post comment
 
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.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor