1 <asp:FileUpload ID="fudTest" runat="server" />
2 <asp:Button ID="btnUpload" runat="server" Text="上传" OnClick="btnUpload_Click" />
3 <br />
4 <asp:Label ID="lblMessage" runat="server"></asp:Label>
5 1.aspx.cs
6
7
8 //上传文件按钮
9 protected void btnUpload_Click(object sender, EventArgs e)
10 {
11 //定义保存路径
12 string savePath = "UploadFiles";
13
14 //是否存在目录
15 if (!System.IO.Directory.Exists(Server.MapPath(savePath)))
16 {
17 //不存在创建文件夹
18 System.IO.Directory.CreateDirectory(Server.MapPath(savePath) );
19 }
20
21 //上传文件
22 if (fudTest.HasFile)
23 {
24 try
25 {
26 fudTest.SaveAs(Server.MapPath(savePath) + "\\" + fudTest.FileName);
27
28 lblMessagelblMessage.Text = lblMessage.Text+"客户端路径:" + fudTest.PostedFile.FileName + "<br>" +
29
30 "文件名:" + System.IO.Path.GetFileName(fudTest.FileName) + "<br>" +
31
32 "文件扩展名:" + System.IO.Path.GetExtension(fudTest.FileName) + "<br>" +
33
34 "文件大小:" + fudTest.PostedFile.ContentLength + " KB<br>" +
35
36 "文件MIME类型:" + fudTest.PostedFile.ContentType + "<br>" +
37
38 "保存路径:" + Server.MapPath(savePath) + "\\" + fudTest.FileName+
39 "<hr>";
40
41 }
42 catch (Exception ex)
43 {
44 lblMessage.Text = "发生错误:" + ex.Message.ToString();
45 }
46 }
47 else
48 {
49 lblMessage.Text = "没有选择要上传的文件!";
50 }
51 }
文件上传控件FileUpload用法初步
时间: 2024-10-10 11:11:15