ASP.NET 2.0 中 FileUpload 服务器控件的使用
在 ASP.NET 1.0/1.1 中,你可以使用 HTML FileUpload 服务器控件(设置
ASP.NET 2.0 提供了一个新的 FileUpload 服务器控件,它使上传文件到服务器的方法更简单。下面是使用此控件的简单代码。
C#
更详细的使用方法,请看这里
runat="server")上传文件。这个控件会放一个 <input type="file"> 元素到你的页面,同时控件会在页面的 <form> 元素中加入 enctype="multipart/form-data" 使用户可以上传文件。ASP.NET 2.0 提供了一个新的 FileUpload 服务器控件,它使上传文件到服务器的方法更简单。下面是使用此控件的简单代码。
C#
1 <%@ Page Language="C#"%>
2 <script runat="server">
3 protected void Button1_Click(object sender, EventArgs e)
4 {
5 if (FileUpload1.HasFile)
6 try {
7 FileUpload1.SaveAs("C:\\Uploads\\" + FileUpload1.FileName);
8 Label1.Text = "File name: " +
9 FileUpload1.PostedFile.FileName + "<br>" +
10 FileUpload1.PostedFile.ContentLength + " kb<br>" +
11 "Content type: " +
12 FileUpload1.PostedFile.ContentType;
13 }
14 catch (Exception ex) {
15 Label1.Text = "ERROR: " + ex.Message.ToString();
16 }
17 else
18 {
19 Label1.Text = "You have not specified a file.";
20 }
21 }
22 </script>
2 <script runat="server">
3 protected void Button1_Click(object sender, EventArgs e)
4 {
5 if (FileUpload1.HasFile)
6 try {
7 FileUpload1.SaveAs("C:\\Uploads\\" + FileUpload1.FileName);
8 Label1.Text = "File name: " +
9 FileUpload1.PostedFile.FileName + "<br>" +
10 FileUpload1.PostedFile.ContentLength + " kb<br>" +
11 "Content type: " +
12 FileUpload1.PostedFile.ContentType;
13 }
14 catch (Exception ex) {
15 Label1.Text = "ERROR: " + ex.Message.ToString();
16 }
17 else
18 {
19 Label1.Text = "You have not specified a file.";
20 }
21 }
22 </script>
更详细的使用方法,请看这里
posted on 2006-08-23 23:03 Easy Company 阅读(1058) 评论(0) 收藏 举报
浙公网安备 33010602011771号