In previous Web applications, uploading files was a very troublesome task. Now, with. NET, File Upload becomes easy. The following example implements the Multifile upload function. You can dynamically add input forms. There is no limit on the number of uploaded files. The Code is as follows:
MultiUpload. aspx
<% @ Page Language = "vb" AutoEventWireup = "false" Codebehind = "MultiUpload. aspx. vb"
Inherits = "aspxWeb. MultiUpload" %>
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<Title> Multifile upload </title>
<Script language = "JavaScript">
Function addFile ()
{
Var str = '<INPUT type = "file" size = "50" NAME = "File">'
Document. getElementById ('myfile'). insertAdjacentHTML ("beforeEnd", str)
}
</Script>
</HEAD>
<Body>
<Form id = "form1" method = "post" runat = "server" enctype = "multipart/form-data">
<Center>
<Asp: Label Runat = "server" ID = "MyTitle"> </asp: Label>
<P id = "MyFile"> <INPUT type = "file" size = "50" NAME = "File"> </P>
<P>
<Input type = "button" value = "Add" onclick = "addFile ()">
<Asp: Button Runat = "server" Text = "Upload" ID = "Upload"> </asp: Button>
<Input onclick = "this. form. reset ()" type = "button" value = "ReSet (reset)">
</P>
</Center>
<P align = "center">
<Asp: Label id = "strStatus" runat = "server" Font-Names = "" Font-Bold = "True"
Font-Size = "9pt" Width = "500px" BorderStyle = "None" BorderColor = "White"> </asp: Label>
</P>
</Form>
</Body>
</HTML>
Code: MultiUpload. aspx. vb
Public Class MultiUpload
Inherits System. Web. UI. Page
Protected WithEvents Upload As System. Web. UI. WebControls. Button
Protected WithEvents MyTitle As System. Web. UI. WebControls. Label
Protected WithEvents strStatus As System. Web. UI. WebControls. Label
# Region "Web Form Designer Generated Code"
'This call is required by the Web Form Designer.
<System. Diagnostics. DebuggerStepThrough ()> Private Sub InitializeComponent ()
End Sub
Private Sub Page_Init (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles MyBase. Init
'Codegen: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent ()
End Sub
# End Region
Private Sub Page_Load (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles MyBase. Load
MyTitle. Text = "Upload. Text = "Start Upload"
If (Me. IsPostBack) Then Me. SaveImages ()
End Sub
Private Function SaveImages () As System. Boolean
'Traverse File form elements
Dim files As System. Web. HttpFileCollection = System. Web. HttpContext. Current. Request. Files
'Status Information
Dim strMsg As New System. Text. StringBuilder ("the uploaded files are: Dim iFile As System. Int32
Try
For iFile = 0 To files. Count-1
'Check the extension name of the file.
Dim postedFile As System. Web. HttpPostedFile = files (iFile)
Dim fileName, fileExtension As System. String
FileName = System. IO. Path. GetFileName (postedFile. FileName)
If Not (fileName = String. Empty) Then
FileExtension = System. IO. Path. GetExtension (fileName)
StrMsg. Append ("type of the uploaded file:" + postedFile. ContentType. ToString () + "<br> ")
StrMsg. Append ("client file address:" + postedFile. FileName + "<br> ")
StrMsg. Append ("file name to be uploaded:" + fileName + "<br> ")
StrMsg. Append ("File Upload Extension:" + fileExtension + "<br> 'Different folders can be saved according to different extension names.
PostedFile. SaveAs (System. Web. HttpContext. Current. Request. MapPath ("images/") + fileName)
End If
Next
StrStatus. Text = strMsg. ToString ()
Return True
Catch Ex As System. Exception
StrStatus. Text = Ex. Message
Return False
End Try
End Function
End Class
C # version
UpLoad. aspx
<% @ Page language = "c #" Codebehind = "UpLoad. aspx. cs" AutoEventWireup = "false" Inherits = "WebPortal. Upload" %>
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<Title> Multifile upload </title>
<Script language = "JavaScript">
Function addFile ()
{
Var str = '<INPUT type = "file" size = "50" NAME = "File">'
Document. getElementById ('myfile'). insertAdjacentHTML ("beforeEnd", str)
}
</Script>
</HEAD>
<Body>
<Form id = "form1" method = "post" runat = "server" enctype = "multipart/form-data">
<Div align = "center">
<H3> Multifile upload <P id = "MyFile"> <INPUT type = "file" size = "50" NAME = "File"> </P>
<P>
<Input type = "button" value = "Add" onclick = "addFile ()">
<Input onclick = "this. form. reset ()" type = "button" value = "ReSet (reset)">
<Asp: Button Runat = "server" Text = "Start upload" ID = "UploadButton"> </asp: Button>
</P>
<P>
<Asp: Label id = "strStatus" runat = "server" Font-Names = "" Font-Bold = "True" Font-Size = "9pt"
Width = "500px" BorderStyle = "None" BorderColor = "White"> </asp: Label>
</P>
</Div>
</Form>
</Body>
</HTML>
UpLoad. aspx. cs
Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Web;
Using System. Web. SessionState;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. HtmlControls;
Namespace WebPortal
{
/// <Summary>
/// Summary of UpLoad.
/// Multifile upload
/// </Summary>
Public class Upload: System. Web. UI. Page
{
Protected System. Web. UI. WebControls. Button UploadButton;
Protected System. Web. UI. WebControls. Label strStatus;
Private void Page_Load (object sender, System. EventArgs e)
{
/// Place user code here to initialize the page
If (this. IsPostBack) this. SaveImages ();
}
Private Boolean SaveImages ()
{
/// 'Traverse the File form Element
HttpFileCollection files = HttpContext. Current. Request. Files;
/// 'Status Information
System. Text. StringBuilder strMsg = new System. Text. StringBuilder ();
StrMsg. Append ("the uploaded files are: Try
{
For (int iFile = 0; iFile <files. Count; iFile ++)
{
/// 'Check the extension name of the file
HttpPostedFile postedFile = files [iFile];
String fileName, fileExtension;
FileName = System. IO. Path. GetFileName (postedFile. FileName );
If (fileName! = "")
{
FileExtension = System. IO. Path. GetExtension (fileName );
StrMsg. Append ("type of the uploaded file:" + postedFile. ContentType. ToString () + "<br> ");
StrMsg. Append ("client file address:" + postedFile. FileName + "<br> ");
StrMsg. Append ("file name to be uploaded:" + fileName + "<br> ");
StrMsg. Append ("File Upload Extension:" + fileExtension + "<br> /// 'Can be saved to different folders based on different extension names
/// Note: you may need to modify the anonymous write permission of your folder.
PostedFile. SaveAs (System. Web. HttpContext. Current. Request. MapPath ("images/") + fileName );
}
}
StrStatus. Text = strMsg. ToString ();
Return true;
}
Catch (System. Exception Ex)
{
StrStatus. Text = Ex. Message;
Return false;
}
}
# Code generated by region Web Form Designer
Override protected void OnInit (EventArgs e)
{
//
// CODEGEN: This call is required by the ASP. NET Web form designer.
//
InitializeComponent ();
Base. OnInit (e );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. ID = "Upload ";
This. Load + = new System. EventHandler (this. Page_Load );
}
# Endregion
}
}