asp.net multiple file uploads using the HTML control's file control, you need to add the "enctype=" Multipart/form-data "" in the form.
Up3.aspx File Code
Copy Code code as follows:
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Up3.aspx.cs" inherits= "Up3"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<script language= "javascript" type= "Text/javascript" >
function AddFile () {
var odiv = document.getElementById ("MyFile");
var str = "<div><input name= ' file ' type= ' file '/></div>";
Odiv.insertadjacenthtml ("BeforeEnd", str);
}
function Resetfile () {
var odiv = document.getElementById ("MyFile");
odiv.innerhtml = "<div><input name= ' file ' type= ' file '/></div>";
}
</script>
<body>
<form id= "Form1" runat= "Server" enctype= "Multipart/form-data" >
<input type= "button" value= "Add" onclick= "AddFile ()"/>
<input type= "button" value= "reset" onclick= "Resetfile ()"/>
<div id= "MyFile" >
<div><input name= "File" type= "file"/></div>
</div>
<asp:button runat= "Server" text= "upload" id= "Button1" onclick= "Button1_Click" bordercolor= "Desktop"
Borderwidth= "1px" height= "20px" width= "60px" ></asp:Button>
<div>
<asp:label id= "Label1" runat= "Server" ></asp:Label>
</div>
</form>
</body>
Up3.aspx.cs File Code
Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.IO;
public partial class Up3:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
}
protected void Button1_Click (object sender, EventArgs e)
{
String Uppath = "/up/"; Upload file path
int uplength = 5; Upload file size
String upfileextname = "|bmp|jpg|jpeg|png|gif|";
Httpfilecollection _files = System.Web.HttpContext.Current.Request.Files;
int flag = _files. Count;
int flagn = 0;
int flage = 0;
int flagee = 0;
String flageestr = "";
for (int i = 0; i < _files. Count; i++)
{
String name = _files[i]. FileName;
FileInfo fi = new FileInfo (name);
String oldfilename = fi. Name;
String scextension = fi. Extension.tolower ();
String fileName = DateTime.Now.ToString ("yyyymmddhhmmssfff") + FI. Extension; File name, current time (YYYYMMDDHHMMSSFFF)
String webfilepath = Server.MapPath (uppath) + fileName; Server-side file path
if (Upfileextname.indexof (Scextension.replace (".", "")) = = 1)
{
Flagee = Flagee + 1;
Flageestr = Flageestr + "No." + (i + 1) + "file, filename [" + Oldfilename + "], file type does not match! ";
Continue
}
if (FI. Length/(1024 * 1024)) > Uplength)
{
Flagee = Flagee + 1;
Flageestr = Flageestr + "" "+ (i + 1) +" file, filename ["+ Oldfilename +"], exceeding "+ Uplength +" m size limit! ";
Continue
}
Try
{
_files[i]. SaveAs (Webfilepath);
}
catch (Exception ex)
{
Flagee = Flagee + 1;
Flageestr = Flageestr + "First" + (i + 1) + "file, upload Exception" "+ex. Message+ "" ";
}
}
Label1.Text = "Total file" "+ Flag +" ", Upload Success File" "+ Flagn +" ", Exception File" "+ (Flage + flagee) +" "" + Flageestr + "" ";
}
}