项目实践之在ASP.NET中实现多文件上传
2007-09-18 22:09
381 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<%@ Page language="c#" Codebehind="UpLoad.aspx.cs" AutoEventWireup="false" Inherits="WebPortal.Upload" %>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<HTML>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<HEAD>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<title>多文件上传</title>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<script language="JavaScript">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
function addFile()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var str = '<INPUT type="file" size="50" NAME="File">'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</HEAD>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<form id="form1" method="post" runat="server" enctype="multipart/form-data">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<div align="center">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<h3>多文件上传</h3>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<P id="MyFile"><INPUT type="file" size="50" NAME="File"></P>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<P>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<input type="button" value="增加(Add)" onclick="addFile()">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<input onclick="this.form.reset()" type="button" value="重置(ReSet)">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:Button Runat="server" Text="开始上传" ID="UploadButton"></asp:Button>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</P>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<P>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:Label id="strStatus" runat="server" Font-Names="宋体" Font-Bold="True" Font-Size="9pt"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Width="500px" BorderStyle="None" BorderColor="White"></asp:Label>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</P>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</div>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</form>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</HTML>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
UpLoad.aspx.cs
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Collections;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.ComponentModel;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Data;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Drawing;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.SessionState;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.HtmlControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
namespace WebPortal
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// UpLoad 的摘要说明。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 实现多文件上传
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public class Upload : System.Web.UI.Page
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected System.Web.UI.WebControls.Button UploadButton;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected System.Web.UI.WebControls.Label strStatus;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void Page_Load(object sender, System.EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// 在此处放置用户代码以初始化页面
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (this.IsPostBack) this.SaveImages();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Boolean SaveImages()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**////'遍历File表单元素
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HttpFileCollection files = HttpContext.Current.Request.Files;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// '状态信息
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
strMsg.Append("上传的文件分别是:<hr color=red>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int iFile = 0; iFile < files.Count; iFile++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**////'检查文件扩展名字
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HttpPostedFile postedFile = files[iFile];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string fileName, fileExtension;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fileName = System.IO.Path.GetFileName(postedFile.FileName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (fileName != "")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
fileExtension = System.IO.Path.GetExtension(fileName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
strMsg.Append("上传文件的文件名:" + fileName + "<br>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**////'可根据扩展名字的不同保存到不同的文件夹
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
///注意:可能要修改你的文件夹的匿名写入权限。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/") + fileName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
strStatus.Text = strMsg.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
catch(System.Exception Ex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
strStatus.Text = Ex.Message;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
override protected void OnInit(EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
InitializeComponent();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
base.OnInit(e);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/**//// <summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
/// 此方法的内容。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
/// </summary>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private void InitializeComponent()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.ID = "Upload";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.Load += new System.EventHandler(this.Page_Load);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
#endregion
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- ASP.NET项目中使用CKEditor +CKFinder 实现上传图片
- Atitit 文件上传 架构设计 实现机制 解决方案 实践java php c#.net js javascript c++ python
- 在ASP.NET中实现多文件上传
- asp.NET 下真正实现大文件上传
- Asp.net 2.0 用 FileUpload 控件实现多文件上传(用户控件)
- 用Fine Uploader+ASP.NET MVC实现ajax文件上传[代码示例]
- Ext 上传文件,前后台实现, Asp.net 代码
- asp.net下实现支持文件分块多点异步上传的 Web Services
- Asp.net实现MVC处理文件的上传下载功能实例教程
- 在ASP.NET中实现多文件上传
- Asp.net上传文件实践
- ASP.NET中实现多文件上传(普通)
- 在ASP.NET中实现多文件上传
- 在ASP.NET MVC中实现大文件异步上传
- Asp.net实现文件上传
- 在ASP.NET中实现多文件上传-孟宪会
- ASP.NET实现多文件上传
- 【原创】Ext 上传文件,前后台实现, Asp.net 代码
- ASP.NET Mvc Preview 5 演示Demo #3 实现MVC批量上传文件
- asp.net(c#)实现文件上传功能