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%"> </div><tr> </table><td style="width: 100px"> </tr>Single File Upload:<br /> <td style="width: 100px"> <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> <asp:GridView ID="UploadedFiles" DataSource="<%# GetUploadList() %>" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None"> </td><FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> </asp:GridView><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" /> </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 End Class Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then End SubUploadedFiles.DataBind() End IfProtected Function GetUploadList() As String() Dim folder As String = Server.MapPath("~/Uploads") End FunctionDim 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)) NextReturn fileNames Protected Sub UploadThisFile(ByVal upload As FileUpload) If upload.HasFile Then End SubDim theFileName As String = Path.Combine(Server.MapPath("~/Uploads"), upload.FileName) End IfIf File.Exists(theFileName) Then File.Delete(theFileName) End Ifupload.SaveAs(theFileName) Protected Sub buttonUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonUpload.Click UploadThisFile(FileUpload1) End SubUploadedFiles.DataBind() Protected Sub buttonMultiUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles buttonMultiUpload.Click UploadThisFile(multiUpload1) End SubUploadThisFile(multiUpload2) UploadThisFile(multiUpload3) UploadedFiles.DataBind() |
No comments:
Post a Comment