Upload.aspx
Copy Code code as follows:
<%@ 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> multi-File upload </title>
<script language= "JavaScript" >
function AddFile ()
{
var str = ' <input type= "file" size= "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" >
<p id= "MyFile" ><input type= "file" size= "$" name= "file" ></P>
<P>
<input type= "button" value= "Add" onclick= "AddFile ()" >
<input onclick= "This.form.reset ()" type= "button" value= "Reset" >
<asp:button runat= "Server" text= "Start uploading" id= "Uploadbutton" ></asp:Button>
</P>
<P>
<asp:label id= "strstatus" runat= "server" font-names= "Arial" font-bold= "True" font-size= "9pt"
Width= "500px" borderstyle= "None" bordercolor= "white" ></asp:Label>
</P>
</div>
</form>
</body>
</HTML>
Upload.aspx.cs
Copy Code code as follows:
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);
}
#endregion
}
}