ASP.NET使用FileUpload上传文件
前台代码:
- <asp:FileUpload ID="fuKeleyi" runat="server" />
- <asp:Button ID="BtnUp" runat="server" onclick="BtnUp_Click" Text="上 传" />
- <asp:Label ID="LabMsg" runat="server"></asp:Label>
后台C#代码:
- using System;
- using System.Collections.Generic;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace docnet
- {
- public partial class up : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void BtnUp_Click(object sender, EventArgs e)
- {
- if (fuKeleyi.HasFile)
- {
- string savePath = Server.MapPath("~/hovertreeupload/");//指定上传文件在服务器上的保存路径
- //检查服务器上是否存在这个物理路径,如果不存在则创建
- if (!System.IO.Directory.Exists(savePath))
- {
- System.IO.Directory.CreateDirectory(savePath);
- }
- savePath = savePath + "\\" + fuKeleyi.FileName;
- fuKeleyi.SaveAs(savePath);
- LabMsg.Text = string.Format("<a href='hovertreeupload/{0}'>hovertreeupload/{0}</a>", fuKeleyi.FileName);
- }
- else
- {
- LabMsg.Text = "你还没有选择上传文件!";
- }
- }
- }
- }