ASP.NET基本控件FileUpload上传控件

前台代码:

<asp:FileUpload ID="FileUpload" runat="server" />
<asp:Button ID="BtnUp" runat="server" onclick="BtnUp_Click" Text="上 传" />
<asp:Label ID="LabMsg" runat="server"></asp:Label>


后台代码:

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 (FileUpload.HasFile)
            {
                string savePath = Server.MapPath("~/upload/");//指定上传文件在服务器上的保存路径
                //检查服务器上是否存在这个物理路径,如果不存在则创建
                if (!System.IO.Directory.Exists(savePath))
                {
                    System.IO.Directory.CreateDirectory(savePath);
                }
                savePath = savePath + "\\" + FileUpload.FileName;
                FileUpload.SaveAs(savePath);
                LabMsg.Text = string.Format("<a href='upload/{0}'>upload/{0}</a>", FileUpload.FileName);
            }
            else
            {
                LabMsg.Text = "你还没有选择上传文件!";
            }
        }
    }
}
posted @ 2011-05-09 12:05  快乐的langYa  阅读(12320)  评论(4编辑  收藏  举报