.net 实现多文件上传

转:http://www.cnblogs.com/mgod/archive/2007/04/24/725393.aspx

注:按原文的方法在我机器上测试,不论一次性上传多少个文件,始终为一个文件名,将fileCount*100做为随机数基数传入后问题解决,直接贴出代码:

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UploadTest.aspx.cs" Inherits="Admin_UploadTest" %>

<!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">
<link href="Images/skin.css" rel="stylesheet" type="text/css" />

<script language="JavaScript">
function addFileControl()
{
var str = '<INPUT type="file" NAME="File">'
document.getElementById(
'FileCollection').insertAdjacentHTML("beforeEnd",str)
}
</script>

</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<asp:Panel ID="PanelFileManage" runat="server" Width="100%">
<p id="FileCollection">
<input type="file" name="File">&nbsp;</p>
<p align="center" style="color: Red">
允许上传的类型为:ZIP,RAR,TXT,大小为1M以下</p>
<p align="center">
<input onclick="addFileControl()" type="button" value="增加(File)">
<asp:Button ID="Upload" runat="server" Text="上传" Width="56px" OnClick="Upload_Click">
</asp:Button>
<input style="width: 56px; height: 24px" onclick="this.form.reset()" type="button"
value
="重置">
</p>
<p align="center">
<asp:Label ID="strStatus" runat="server" BorderColor="White" BorderStyle="None" Width="500px"
Font-Size
="9pt" Font-Bold="True" Font-Names="宋体"></asp:Label></p>
</asp:Panel>
</form>
</body>
</html>

后台:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin_UploadTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected bool upMorefile()
{

//遍历File表单元素
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
//状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder("成功上传的文件信息分别为:<hr color=red>");
int fileCount;
int filecount = files.Count;


try
{
for (fileCount = 0; fileCount < files.Count; fileCount++)
{
//定义访问客户端上传文件的对象
System.Web.HttpPostedFile postedFile = files[fileCount];
string FileType = postedFile.ContentType.ToString();//获取要上传的文件类型,验证文件头

string fileName, fileExtension;
//取得上传得文件名
fileName = System.IO.Path.GetFileName(postedFile.FileName);
//取得文件的扩展名
fileExtension = System.IO.Path.GetExtension(fileName);

//在上传文件不为空的情况下,验证文件名以及大小是否符合,如果不符合则不允许上传
if (((FileType == "image/pjpeg" && fileExtension.ToLower() == ".jpg") || (FileType == "image/gif" && fileExtension.ToLower() == ".gif") || (FileType == "application/octet-stream" && fileExtension.ToLower() == ".rar")) && postedFile.ContentLength / 1024 <= 1024)
{//在这里通过检查文件头与文件名是否匹配 从而限制了文件上传类型 注:可上传的类型有TXT,ZIP,RAR,且大小只能为1M一下

if (fileName != String.Empty)
{
fileName = RandomFileName(fileCount) + fileExtension;
//上传的文件信息
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + fileName + "<br>");
strMsg.Append("上传文件的大小为:" + postedFile.ContentLength + "字节<br>");
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr color=red>");
//保存到指定的文件夹
postedFile.SaveAs(Server.MapPath("../template/") + fileName);

fileName = "";

}
}
else
{
strStatus.Text += "" + (fileCount + 1) + "个文件不符合要求<br/> ";

}

}

strStatus.Text += strMsg.ToString();
return true;
}
catch (System.Exception error)
{
strStatus.Text = error.Message;
return false;

}
}


protected void Upload_Click(object sender, EventArgs e)
{
strStatus.Text = "";//讲提示信息清空
upMorefile();//调用上传类
}

public string RandomFileName(int i)
{//返回随机数的类
string filename = "";
string r1 = "";
string r2 = "";
string r4 = "";
Random random = new Random(i * 100);
r1 = ((char)random.Next(65, 90)).ToString();//大写字母
r2 = ((char)random.Next(97, 122)).ToString();//小写字母
r4 = random.Next(10000, 999999).ToString();
filename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + r1 + r4 + r2 + r1 + r4 + r1;
return filename;
}
}



你可能感兴趣的:(多文件上传)