In previous Web applications, uploading files was a hassle, and now with. NET, file uploads have become a breeze. The following example implements the multiple file upload function. You can dynamically add input forms, and the number of uploaded files is unlimited. The code is as follows:
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 are 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-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 Uploading"
If (Me.ispostback) Then me.saveimages ()
End Sub
Private Function saveimages () as System.Boolean
' Traverse the file Form element
Dim files as System.Web.HttpFileCollection = System.Web.HttpContext.Current.Request.Files
' Status information
Dim STRMSG as New System.Text.StringBuilder ("uploaded files are: Dim IFile as System.Int32
Try
For ifile = 0 to files. Count-1
' Check file extension name
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 ("uploaded file type:" + postedFile.ContentType.ToString () + "<br>")
Strmsg.append ("Client file Address:" + postedfile.filename + "<br>")
Strmsg.append ("FileName of uploaded file:" + filename + "<br>")
Strmsg.append ("Upload file extension:" + fileextension + "<br>' Can be saved to a different folder depending on the extended name
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
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 description of the UpLoad.
Implementing multiple file Uploads
</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 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 ("uploaded files are: Try
{
for (int ifile = 0; ifile < files. Count; ifile++)
{
' Check file extension name
Httppostedfile postedfile = Files[ifile];
String FileName, FileExtension;
FileName = System.IO.Path.GetFileName (postedfile.filename);
if (FileName!= "")
{
FileExtension = System.IO.Path.GetExtension (fileName);
Strmsg.append ("uploaded file type:" + postedFile.ContentType.ToString () + "<br>");
Strmsg.append ("Client file Address:" + postedfile.filename + "<br>");
Strmsg.append ("FileName of uploaded file:" + filename + "<br>");
Strmsg.append ("Upload file extension:" + fileextension + "<br>' Can be saved to a different folder depending on the extended name
Note: You may want to modify the anonymous write permission for 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 #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This.id = "Upload";
This. Load + = new System.EventHandler (this. Page_Load);
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.