前台页面可根据自己需要进行修改,获取后台上传文件类型,文件地址等等可参考后台注释输出代码,也可以自己再做其他需要的定义:
uploadfile.aspx
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="uploadfile.aspx.cs" Inherits="uploadfile" %>
<!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 id="Head1" runat="server">
<title>多文件上传</title>
<script language="javascript">
function addFile()
{
var str = '<input type="file" size ="50" name="File" />';
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str);
}
</script>
</head>
<body>
<form id="form1" runat="server" method="post" enctype="multipart/form-data">
<div>
<p id="MyFile"><input type="file" size="50" name="File" /></p>
<p>
<input type="button" value="增加" onclick="addFile()" />
<input onclick="this.form.reset()" type="button" value="重置" />
<asp:Button ID="UploadButton" Text="开始上传" runat="server" OnClick="UploadButton_Click" />
</p>
<p>
<asp:Label ID="lblStatus" runat="server"></asp:Label>
</p>
</div>
</form>
</body>
</html>后台uploadfile.aspx.cs
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
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 uploadfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private Boolean SaveImages()
{
//遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
//状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
strMsg.Append("上传的文件分别是:<hr color=#FF0000 />");
try
{
for (int iFile = 0; iFile < files.Count; iFile++ )
{
//检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
string file_id;
//取出精确到毫秒的时间做文件的名称
int year = System.DateTime.Now.Year;
int month = System.DateTime.Now.Month;
int day = System.DateTime.Now.Day;
int hour = System.DateTime.Now.Hour;
int minute = System.DateTime.Now.Minute;
int second = System.DateTime.Now.Second;
int millisecond = System.DateTime.Now.Millisecond;
string my_file_id = year.ToString() + month.ToString() + day.ToString() + hour.ToString() + minute.ToString() + second.ToString() + millisecond.ToString() + iFile.ToString();
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (!string.IsNullOrEmpty(fileName))
{
fileExtension = System.IO.Path.GetExtension(fileName);
file_id = my_file_id + fileExtension;
//strMsg.Append("上传的文件类型:"+ postedFile.ContentType.ToString() +"
");
//strMsg.Append("客户端文件地址:"+ postedFile.FileName +"
");
//strMsg.Append("上传文件的文件名:"+ fileName +"
");
//strMsg.Append("上传文件的扩展名:"+ fileExtension +"
");
strMsg.Append("文件名:" + file_id + "<br/><hr />");
//strMsg.Append("文件名:" + fileName + "<br/><hr />");
//可根据扩展名字的不同保存到不同的文件夹
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("ChartImages/") + file_id);
string filespath = System.Web.HttpContext.Current.Server.MapPath("ChartImages/") + file_id;
}
}
lblStatus.Text = strMsg.ToString();
return true;
}
catch (Exception ex)
{
lblStatus.Text = ex.Message;
return false;
}
}
protected void UploadButton_Click(object sender, EventArgs e)
{
Boolean uploadsuccess = SaveImages();
if (!uploadsuccess)
lblStatus.Text = "上传失败!";
}
}
uploadfile.aspx
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="uploadfile.aspx.cs" Inherits="uploadfile" %>
<!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 id="Head1" runat="server">
<title>多文件上传</title>
<script language="javascript">
function addFile()
{
var str = '<input type="file" size ="50" name="File" />';
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str);
}
</script>
</head>
<body>
<form id="form1" runat="server" method="post" enctype="multipart/form-data">
<div>
<p id="MyFile"><input type="file" size="50" name="File" /></p>
<p>
<input type="button" value="增加" onclick="addFile()" />
<input onclick="this.form.reset()" type="button" value="重置" />
<asp:Button ID="UploadButton" Text="开始上传" runat="server" OnClick="UploadButton_Click" />
</p>
<p>
<asp:Label ID="lblStatus" runat="server"></asp:Label>
</p>
</div>
</form>
</body>
</html>
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
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 uploadfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private Boolean SaveImages()
{
//遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
//状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
strMsg.Append("上传的文件分别是:<hr color=#FF0000 />");
try
{
for (int iFile = 0; iFile < files.Count; iFile++ )
{
//检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
string file_id;
//取出精确到毫秒的时间做文件的名称
int year = System.DateTime.Now.Year;
int month = System.DateTime.Now.Month;
int day = System.DateTime.Now.Day;
int hour = System.DateTime.Now.Hour;
int minute = System.DateTime.Now.Minute;
int second = System.DateTime.Now.Second;
int millisecond = System.DateTime.Now.Millisecond;
string my_file_id = year.ToString() + month.ToString() + day.ToString() + hour.ToString() + minute.ToString() + second.ToString() + millisecond.ToString() + iFile.ToString();
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (!string.IsNullOrEmpty(fileName))
{
fileExtension = System.IO.Path.GetExtension(fileName);
file_id = my_file_id + fileExtension;
//strMsg.Append("上传的文件类型:"+ postedFile.ContentType.ToString() +"
");
//strMsg.Append("客户端文件地址:"+ postedFile.FileName +"
");
//strMsg.Append("上传文件的文件名:"+ fileName +"
");
//strMsg.Append("上传文件的扩展名:"+ fileExtension +"
");
strMsg.Append("文件名:" + file_id + "<br/><hr />");
//strMsg.Append("文件名:" + fileName + "<br/><hr />");
//可根据扩展名字的不同保存到不同的文件夹
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("ChartImages/") + file_id);
string filespath = System.Web.HttpContext.Current.Server.MapPath("ChartImages/") + file_id;
}
}
lblStatus.Text = strMsg.ToString();
return true;
}
catch (Exception ex)
{
lblStatus.Text = ex.Message;
return false;
}
}
protected void UploadButton_Click(object sender, EventArgs e)
{
Boolean uploadsuccess = SaveImages();
if (!uploadsuccess)
lblStatus.Text = "上传失败!";
}
}