前台代码:Default.aspx 注意:一定要在<form id="form1" runat="server" enctype="multipart/form-data">
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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 strFile='<input name="File1" type="file" /><br />';
document.getElementById("DivFile").insertAdjacentHTML("beforeEnd",strFile);
}
</script>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<div>
<div id="DivFile"><input name="File1" type="file" /><br />
</div>
<input id="Button2" type="button" value="增加" onclick="return AddFile();" />
<asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" /><br />
</div>
</form>
</body>
</html>
<!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 strFile='<input name="File1" type="file" /><br />';
document.getElementById("DivFile").insertAdjacentHTML("beforeEnd",strFile);
}
</script>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<div>
<div id="DivFile"><input name="File1" type="file" /><br />
</div>
<input id="Button2" type="button" value="增加" onclick="return AddFile();" />
<asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" /><br />
</div>
</form>
</body>
</html>
后台代码:Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
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;
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//这是第一种写法了
////定义上传文件的列表
//HttpFileCollection fileList = HttpContext.Current.Request.Files;
////存放上传文件的操作信息
//StringBuilder strMessage = new StringBuilder("当前上传的文件分别是:<hr color=red>");
//try
//{
// //上传文件列表中的每一个文件
// for (int i = 0; i < fileList.Count; i++)
// {
// //取得当前上传的文件
// HttpPostedFile postedFile = fileList[i];
// string fileName;
// string fileExtension;
// //获取上传文件的文件名
// if (postedFile.ContentLength > 0)
// {
// fileName = Path.GetFileName(postedFile.FileName);
// fileExtension = Path.GetExtension(fileName);
// strMessage.Append("上传文件类型:" + postedFile.ContentType.ToString() + "<br />");
// strMessage.Append("客户端文件地址:" + postedFile.FileName + "<br/>");
// strMessage.Append("上传文件的文件名:" + fileName + "<br/>");
// strMessage.Append("上传文件的扩展名:" + fileExtension + "<br/></hr>");
// string path = Server.MapPath("files\\") + fileName;
// postedFile.SaveAs(path);
// Response.Write(strMessage.ToString());
// }
// else
// {
// Response.Write("不能上传空文件");
// }
// }
//}
//catch (Exception ex)
//{
// throw ex;
//}
StringBuilder strMessage = new StringBuilder();
HttpFileCollection fileList = HttpContext.Current.Request.Files;
for (int i = 0; i < fileList.Count; i++)
{
HttpPostedFile postedFile = fileList[i];
if (postedFile.ContentLength > 0)
{
string FileName = postedFile.FileName;
string FileExtension = FileName.Substring(FileName.LastIndexOf(".") + 1);
string[] NoFileExtension = { "exe", "asp", "php" };
bool IsUp = true;
for (int j = 0; j < NoFileExtension.Length; j++)
{
if (FileExtension.Equals(NoFileExtension[j]))
{
IsUp = false;
}
}
if (IsUp)
{
//循环时在文件名后面加变量i,防止同时传相同类型的文件时,后一个文件覆盖前一个文件
string sFilePath = string.Format("files/{0}.{1}", DateTime.Now.ToString("mmhhddss")+i, FileExtension);
postedFile.SaveAs(Server.MapPath(sFilePath));
strMessage.Append("文件上传成功!");
strMessage.Append(string.Format("{0}.{1}", DateTime.Now.ToString("mmhhddss")+i, FileExtension));
strMessage.Append("<br />");
strMessage.Append("文件的格式:");
strMessage.Append(FileExtension+"<br />");
strMessage.Append("<br />```````````````<br />");
Response.Write("文件上传成功!" + "<br />文件名是:" + string.Format("{0}.{1}", DateTime.Now.ToString("mmhhddss")+i, FileExtension) + "<br />文件的格式:" + FileExtension);
}
else
{
Response.Write(string.Format("不允许上传{0}格式文件", FileExtension));
}
}
else
{
Response.Write("不能上传空文件");
}
}
}
}
using System.Data;
using System.Configuration;
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;
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//这是第一种写法了
////定义上传文件的列表
//HttpFileCollection fileList = HttpContext.Current.Request.Files;
////存放上传文件的操作信息
//StringBuilder strMessage = new StringBuilder("当前上传的文件分别是:<hr color=red>");
//try
//{
// //上传文件列表中的每一个文件
// for (int i = 0; i < fileList.Count; i++)
// {
// //取得当前上传的文件
// HttpPostedFile postedFile = fileList[i];
// string fileName;
// string fileExtension;
// //获取上传文件的文件名
// if (postedFile.ContentLength > 0)
// {
// fileName = Path.GetFileName(postedFile.FileName);
// fileExtension = Path.GetExtension(fileName);
// strMessage.Append("上传文件类型:" + postedFile.ContentType.ToString() + "<br />");
// strMessage.Append("客户端文件地址:" + postedFile.FileName + "<br/>");
// strMessage.Append("上传文件的文件名:" + fileName + "<br/>");
// strMessage.Append("上传文件的扩展名:" + fileExtension + "<br/></hr>");
// string path = Server.MapPath("files\\") + fileName;
// postedFile.SaveAs(path);
// Response.Write(strMessage.ToString());
// }
// else
// {
// Response.Write("不能上传空文件");
// }
// }
//}
//catch (Exception ex)
//{
// throw ex;
//}
StringBuilder strMessage = new StringBuilder();
HttpFileCollection fileList = HttpContext.Current.Request.Files;
for (int i = 0; i < fileList.Count; i++)
{
HttpPostedFile postedFile = fileList[i];
if (postedFile.ContentLength > 0)
{
string FileName = postedFile.FileName;
string FileExtension = FileName.Substring(FileName.LastIndexOf(".") + 1);
string[] NoFileExtension = { "exe", "asp", "php" };
bool IsUp = true;
for (int j = 0; j < NoFileExtension.Length; j++)
{
if (FileExtension.Equals(NoFileExtension[j]))
{
IsUp = false;
}
}
if (IsUp)
{
//循环时在文件名后面加变量i,防止同时传相同类型的文件时,后一个文件覆盖前一个文件
string sFilePath = string.Format("files/{0}.{1}", DateTime.Now.ToString("mmhhddss")+i, FileExtension);
postedFile.SaveAs(Server.MapPath(sFilePath));
strMessage.Append("文件上传成功!");
strMessage.Append(string.Format("{0}.{1}", DateTime.Now.ToString("mmhhddss")+i, FileExtension));
strMessage.Append("<br />");
strMessage.Append("文件的格式:");
strMessage.Append(FileExtension+"<br />");
strMessage.Append("<br />```````````````<br />");
Response.Write("文件上传成功!" + "<br />文件名是:" + string.Format("{0}.{1}", DateTime.Now.ToString("mmhhddss")+i, FileExtension) + "<br />文件的格式:" + FileExtension);
}
else
{
Response.Write(string.Format("不允许上传{0}格式文件", FileExtension));
}
}
else
{
Response.Write("不能上传空文件");
}
}
}
}