Sunday, 24 July 2011

Simple File Upload Control with ASP.NET and VB


This tutorial will show how to implement the FileUpload control so that files can be uploaded to a website. VB version.

In this tutorial, we will create both a single file upload form and a multi-file upload form.
After adding one FileUpload control for the single file upload and three for the multi-file upload, we two buttons to submit each form. We also add a GridView control to display the files which have been uploaded. Note the DataSource of this GridView.

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

The ASPX code looks something like this:
<form id="form1" runat="server">
<div>
<table style="width: 90%">
<tr>
<td style="width: 100px">
Single File Upload:<br />
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<asp:Button ID="buttonUpload" runat="server" Text="Upload" /><br />
<br />
Multi-File Upload:<br />
<asp:FileUpload ID="multiUpload1" runat="server" /><br />
<asp:FileUpload ID="multiUpload2" runat="server" /><br />
<asp:FileUpload ID="multiUpload3" runat="server" /><br />
<asp:Button ID="buttonMultiUpload" runat="server" Text="Upload" /></td>
<td style="width: 100px">
<asp:GridView ID="UploadedFiles" DataSource="<%# GetUploadList() %>" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

The DataSource of the GridView is a function that collects the file list of the uploads folder, hence listing all the files in that folder in the GridView control.
Both of the Submit buttons run the same function -- UploadThisFile(). The only difference being, the multi-file upload button calls the function three times, each time with the different names of three FileUpload controls. And once the functions have been called, the new data is bound to the GridView, displaying the newly-uploaded files.
The code behind would look something like this:

Imports System.IO

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
UploadedFiles.DataBind()
End If
End Sub

Protected Function GetUploadList() As String()
Dim folder As String = Server.MapPath("~/Uploads")
Dim files() As String = Directory.GetFiles(folder)
Dim fileNames(files.Length - 1) As String
Array.Sort(files)

For i As Integer = 0 To files.Length - 1
fileNames(i) = Path.GetFileName(files(i))
Next

Return fileNames
End Function

Protected Sub UploadThisFile(ByVal upload As FileUpload)
If upload.HasFile Then
Dim theFileName As String = Path.Combine(Server.MapPath("~/Uploads"), upload.FileName)
If File.Exists(theFileName) Then
File.Delete(theFileName)
End If
upload.SaveAs(theFileName)
End If
End Sub

Protected Sub buttonUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonUpload.Click
UploadThisFile(FileUpload1)
UploadedFiles.DataBind()
End Sub

Protected Sub buttonMultiUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonMultiUpload.Click
UploadThisFile(multiUpload1)
UploadThisFile(multiUpload2)
UploadThisFile(multiUpload3)
UploadedFiles.DataBind()
End Sub
End Class

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment