upload.aspx
<%@ Page language="c#" CodeFile="UpLoad.aspx.cs" AutoEventWireup="false" Inherits="UploadFile.Upload" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>文件上传</title>
<script type="JavaScript">
function addFile()
{
var str = '<INPUT type="file" size="50" NAME="File">';
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str);
}
</script>
</head>
<body>
<form id="form1" method="post" runat="server" enctype="multipart/form-data">
<div >
<h3>文件上传</h3>
<p id="MyFile"><input type="file" size="50" name="File"/></p>
<p>
<asp:Button Runat="server" Text="开始上传" ID="UploadButton"></asp:Button>
</p>
<p>
<asp:Label ID="strStatus" runat="server" Font-Names="宋体" Font-Bold="True" Font-Size="9pt" Width="500px" BorderStyle="None" BorderColor="Red"></asp:Label>
<asp:Label ID="lMessage" runat="server" Font-Names="宋体" Font-Bold="True" Font-Size="9pt" Width="500px" BorderStyle="None" BorderColor="Green"></asp:Label>
</p>
</div>
</form>
</body>
</html>
upload.aspx.cs
1 using System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10
11 namespace UploadFile
12 {
13 public partial class Upload : System.Web.UI.Page
14 {
15
16 private void Page_Load(object sender, System.EventArgs e)
17 {
18
19 if (this.IsPostBack) this.SaveFiles();
20 }
21
22 private Boolean SaveFiles()
23 {
24 ///遍历File表单元素
25 HttpFileCollection files = HttpContext.Current.Request.Files;
26 string warnMessae = string.Empty;
27 /// 状态信息
28 System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
29 strMsg.Append("上传的文件分别是:<hr color=red>");
30 try
31 {
32 for (int iFile = 0; iFile < files.Count; iFile++)
33 {
34 HttpPostedFile postedFile = files[iFile];
35 string fileName, fileExtension;
36 fileName = System.IO.Path.GetFileName(postedFile.FileName);
37 if (fileName == "")
38 {
39 warnMessae = "Please choose a file!";
40 lMessage.Text = warnMessae;
41 strStatus.Visible = false;
42 }
43 if (fileName != "")
44 {
45 fileExtension = System.IO.Path.GetExtension(fileName);
46 strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
47 strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
48 strMsg.Append("上传文件的文件名:" + fileName + "<br>");
49 strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");
50 postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("Ave") + fileName);
51 strStatus.Visible = true;
52 lMessage.Visible = false;
53 }
54 }
55 strStatus.Text = strMsg.ToString();
56 return true;
57 }
58 catch (System.Exception Ex)
59 {
60 strStatus.Text = Ex.Message;
61 return false;
62 }
63 }
64 #region WebPage
65 override protected void OnInit(EventArgs e)
66 {
67 InitializeComponent();
68 base.OnInit(e);
69 }
70 private void InitializeComponent()
71 {
72 this.ID = "Upload";
73 this.Load += new System.EventHandler(this.Page_Load);
74
75 }
76 #endregion
77 }
78 }
79