asp.net html control File control to share multiple file upload instances

  • 2020-05-24 05:30:45
  • OfStack

asp.net multi-file upload File controls that use the html control need to be added to form [enctype="multipart/ form-data "].
up3.aspx file code
 
<%@ 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=" increase " 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> 
</html> 

up3.aspx.cs file code
 
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 file path  
if (upFileExtName.IndexOf(scExtension.Replace(".", "")) == -1) 
{ 
flagEE = flagEE + 1; 
flagEEstr = flagEEstr + " The first " + (i + 1) + " Two files, the name of the file [" + oldfilename + "] , file type does not match! "; 
continue; 
} 
if ((fi.Length / (1024 * 1024)) > upLength) 
{ 
flagEE = flagEE + 1; 
flagEEstr = flagEEstr + " The first " + (i + 1) + " Two files, the name of the file [" + oldfilename + "] And beyond " + upLength + "M Size limit! "; 
continue; 
} 
try 
{ 
_files[i].SaveAs(webFilePath); 
} 
catch (Exception ex) 
{ 
flagEE = flagEE + 1; 
flagEEstr = flagEEstr + " The first " + (i + 1) + " File, upload abnormal [ "+ex.Message+" 】 "; 
} 
} 
Label1.Text = " The total file" " + flag + " ], upload the successful file [ " + flagN + " ], exception file [ " + (flagE + flagEE) + "  】  【 " + flagEEstr + " 】 "; 
} 
} 

Related articles: