<input type="file" ID="UploadedFile" runat="server" size="35" NAME="UploadedFile">
.....
protected void Upload_Click(object sender, EventArgs e)
{
// proceed only if a file has been specified
if (UploadedFile.PostedFile != null && UploadedFile.PostedFile.FileName.Length > 0 )
{
string destDir = Server.MapPath(folderPath); try
{
// save the to the current directory
string fileName = Path.GetFileName(UploadedFile.PostedFile.FileName);
UploadedFile.PostedFile.SaveAs(Path.Combine(destDir, fileName));
// refresh the page
Response.Redirect("BrowseFiles.aspx?Folder=" + folderPath);
}
catch (Exception exc)
{
StatusMessage.Text = exc.Message;
StatusMessage.Visible = true;
}
}
}

解决方案 »

  1.   

    如果你用VS.net工具来开发,其自带的工具箱就有该组件。进入工具箱HTML栏,选择file filed组件就可以。然后把其属性改为在服务器端运行即可。
    给你一段上传文件的代码:
    private void Button1_Click(object sender, System.EventArgs e)
    {
    if(this.myFile.PostedFile!=null) //upload textbox
    {
    //接收路径
    string strPath=Request.QueryString["path"]; //current direction //如果接收的路径为空,则获取当前路径
    if(strPath==null)
    {
    strPath=Request.ServerVariables["APPL_PHYSICAL_PATH"];
    }
    else
    {
    if(strPath.Substring(strPath.Length-1,1)!="\\")
    {
    strPath=strPath+"\\";
    }
    } //处理字符串
    string strName=this.myFile.PostedFile.FileName;
    int i=strName.LastIndexOf("\\");
    string strNewName=strName.Substring(i);
    string strFullName=strPath+strNewName;
    //保存文件
    this.myFile.PostedFile.SaveAs(strFullName);
    //Response.Redirect("index.aspx?path="+strPath);
    this.Label2.Text="<center><font color=red>上传成功</font></center>";
    ListFile();
    }
    }
      

  2.   

    How To Upload a File to a Web Server in ASP.NET by Using Visual C# .NET
    http://support.microsoft.com/default.aspx?scid=kb;EN-US;323246