Asp.net: When uploading multiple files, use the File control of the html control to add [enctype = "multipart/form-data"] to form "].
Up3.aspx File Code
Copy codeThe Code is 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">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<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>
</Head>
<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 = "button#click" BorderColor = "Desktop"
BorderWidth = "1px" Height = "20px" Width = "60px"> </asp: Button>
<Div>
<Asp: Label ID = "Label1" runat = "server"> </asp: Label>
</Div>
</Form>
</Body>
</Html>
Up3.aspx. cs File Code
Copy codeThe Code is 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 button#click (object sender, EventArgs e)
{
String upPath = "/up/"; // File Upload path
Int upLength = 5; // size of the uploaded file
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, file name [" + oldfilename + "], the file type does not match! ";
Continue;
}
If (fi. Length/(1024*1024)> upLength)
{
FlagEE = flagEE + 1;
FlagEEstr = flagEEstr + "no." + (I + 1) + "file, file name [" + oldfilename + "], exceeds" + upLength + "M size limit! ";
Continue;
}
Try
{
_ Files [I]. SaveAs (webFilePath );
}
Catch (Exception ex)
{
FlagEE = flagEE + 1;
FlagEEstr = flagEEstr + "no." + (I + 1) + "file, upload exception [" + ex. Message + "]";
}
}
Label1.Text = "total file [" + flag + "], uploaded file [" + flagN + "], abnormal file [" + (flagE + flagEE) + "] [" + flagEEstr + "]";
}
}