Upload
---stylesheet.css
. BlueButton
{
Background-color:lightsteelblue;
Border-style:solid;
border-width:1px;
Border-color:lightskyblue;
}
---attachme.aspx
<%@ Page language= "C #" codebehind= "Attachme.aspx.cs" autoeventwireup= "false" inherits= "Uploadfile.attachme"%>
<HTML>
<HEAD>
<title> multi-File upload </title>
<link href= "Stylesheet.css" rel= "StyleSheet" >
</HEAD>
<body>
<form id= "Attachme" method= "post" enctype= "Multipart/form-data" runat= "Server" >
<input class= "BlueButton" id= "FindFile" type= "file" size= "" runat= "Server" name= "FindFile" >
<BR>
<asp:listbox id= "filelist" runat= "Server" cssclass= "Txtbox" height= "100px" width= "274px" selectionmode= "multiple" ></asp:listbox><BR>
<asp:button id= "AddFile" runat= "Server" cssclass= "BlueButton" height= "23px" width= "72px" text= "Add File" ></asp: Button>
<asp:button id= "Remvfile" runat= "Server" cssclass= "BlueButton" height= "23px" width= "72px" text= "Delete file" ></asp :button>
<input class= "BlueButton" id= "Upload" type= "submit" value= "Upload" runat= "server" onserverclick= "Upload_serverclick"
Name= "Upload" >
</form>
<asp:label id= "Tipinfo" runat= "Server" height= "25px" width= "249px" ></asp:label>
</body>
</HTML>
---Attachme.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 UploadFile
{
///<summary>
///multiple file uploads
/// </summary>
public class Attachme:System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button AddFile;
protected System.Web.UI.WebControls.Button Remvfile;
protected System.Web.UI.HtmlControls.HtmlInputFile FindFile;
protected System.Web.UI.HtmlControls.HtmlInputButton Upload;
protected System.Web.UI.HtmlControls.HtmlGenericControl txtoutput;
protected System.Web.UI.WebControls.ListBox filelist;
protected System.Web.UI.WebControls.Label tipinfo;
static public ArrayList HIF = new ArrayList (); //Save the file list
public int filesuploaded = 0; //the number of uploaded files
private void Page_Load (object sender, System.EventArgs e)
{
}
#region Web Form Designer generated code
override protected void OnInit (EventArgs e)
& NBSP;&NBSP;{&NBSP;&NBSP;&NBSP
initializecomponent ();
base. OnInit (e);
}
///<summary>
///Required The method for Designer support-do is not modify
///the contents of this method with the Code editor.
///</summary>
private void InitializeComponent ()
{
this. Addfile.click + = new System.EventHandler (this. Addfile_click);
this. Remvfile.click + = new System.EventHandler (this. Remvfile_click);
this. Upload.serverclick + = new System.EventHandler (this. Upload_serverclick);
this. Load + = new System.EventHandler (this. Page_Load);
}
#endregion
///<summary>
///Add the file to be uploaded to the listbox
// /</summary>
///<param name= "sender" ></PARAM>
///<param " E "></param>
private void Addfile_click (object sender, System.EventArgs e)
{
if (Page.IsPostBack = = True)
{
hif. ADD (FindFile);
filelist.items.add (FindFile.PostedFile.FileName);
}
else
{}
}
///<summary>
Deletes the specified file from the listbox
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Remvfile_click (object sender, System.EventArgs e)
{
if (Filelist.selectedindex = = 1)
{
Tipinfo.text = "Error-must specify the file to be deleted.";
Return
}
else if (FileList.Items.Count!= 0)
{
HIf. RemoveAt (Filelist.selectedindex);
FileList.Items.Remove (FileList.SelectedItem.Text);
Tipinfo.text = "";
}
}
<summary>
Loop through the files in the listbox to the specified folder
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
public void Upload_serverclick (object sender, System.EventArgs e)
{
String baselocation = Server.MapPath ("uploadfiles/");//upload path
String status = "";//Upload a list of files displayed after success
if ((FileList.Items.Count = 0) && (filesuploaded = = 0))
{
Tipinfo.text = "Error-must specify the file to upload.";
Return
}
Else
{
foreach (System.Web.UI.HtmlControls.HtmlInputFile HIF in HIF)
{
Try
{
STRING fn = System.IO.Path.GetFileName (HIF. Postedfile.filename);
HIF. Postedfile.saveas (Baselocation + fn);
filesuploaded++;
Status = + fn + "<br>";
}
catch (Exception err)
{
Tipinfo.text = "Upload error" + baselocation
+ "<br>" + Err. ToString ();
}
}
if (filesuploaded = = HIf. Count)
{
Tipinfo.text = "A total of uploaded" + filesuploaded + "files. <br> "+ status;
}
HIf. Clear ();
FileList.Items.Clear ();
}
}
}
}
Effect chart