<html>
   <script runat="server">
      Sub UploadBtn_Click(Sender as Object, e as EventArgs)         ' Display information about posted file
         FileName.InnerHtml = MyFile.PostedFile.FileName
         MyContentType.InnerHtml = MyFile.PostedFile.ContentType 
         ContentLength.InnerHtml = cStr(MyFile.PostedFile.ContentLength)
         FileDetails.Visible = True         ' Save uploaded file to server
         MyFile.PostedFile.SaveAs("c:\Uploadedfiles\uploadfile.txt")
      End Sub
   </script>
   <body>
      <form action="fileupload.aspx" 
            method="post"
            enctype="multipart/form-data" 
            runat="server">         <h1>ASP.NET File Upload Example</h1>
         Select File To Upload to Server: 
         <input id="MyFile" 
                type="file" 
                runat="server"> 
         <br><br>
         <input type=submit 
                value="Upload!"
                OnServerclick="UploadBtn_Click" 
                runat="server">
         <br><br><br>
         <div id="FileDetails" 
              Visible=false 
              runat="server">
            FileName: <span id="FileName" runat="server"/> <br>
            ContentType: <span id="MyContentType" runat="server"/> <br>
            ContentLength: <span id="ContentLength" runat="server"/>bytes
            <br>
         </div>
      </form>
   </body>
</html>

解决方案 »

  1.   

    我的简单的usercontrol:UpLoadControl.ascx
    <%@ Control Language="c#" AutoEventWireup="false" Codebehind="UpLoadControl.ascx.cs" Inherits="myUpLoad.UpLoadControl" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
    <FONT face="宋体">
    <form id="upload" method="post" runat="server" enctype="multipart/form-data">
    <TABLE id="Table1" style="WIDTH: 249px; HEIGHT: 68px" cellSpacing="0" borderColorDark="#ffffff" cellPadding="0" width="249" borderColorLight="#000033" border="1">
    <TR>
    <TD colSpan="2">请选择上传到服务器的文件名称:</TD>
    </TR>
    <TR>
    <TD colSpan="2"><INPUT id="upLoadFile" type="file" runat="server" NAME="upLoadFile"></TD>
    </TR>
    <TR>
    <TD colSpan="2" align="middle"><asp:Button id="upLoadButton" runat="server" Text="UpLaod"></asp:Button></TD>
    </TR>
    </TABLE>
    <br>
    <asp:Label id="fileNameLabel" runat="server" Width="481px"></asp:Label><br>
    <asp:Label id="fileTypeLabel" runat="server" Width="477px"></asp:Label><br>
    <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="请您选择上传的文件,先!" ControlToValidate="upLoadFile"></asp:RequiredFieldValidator>
    </form>
    </FONT>
      

  2.   

    codebehind代码,限制为最大传4mnamespace myUpLoad
    {
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.IO; /// <summary>
    /// UpLoadControl 的摘要说明。
    /// </summary>
    public abstract class UpLoadControl : System.Web.UI.UserControl
    {
    protected System.Web.UI.WebControls.Button upLoadButton;
    protected System.Web.UI.WebControls.Label fileNameLabel;
    protected System.Web.UI.WebControls.Label fileTypeLabel;
    protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
    protected System.Web.UI.HtmlControls.HtmlInputFile upLoadFile; private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// 设计器支持所需的方法 - 不要使用
    /// 代码编辑器修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.upLoadButton.Click += new System.EventHandler(this.upLoadButton_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void upLoadButton_Click(object sender, System.EventArgs e)
    {
    string upLoadFileName;
    string upLoadFilePath;
    string upLoadFileToDir; upLoadFileToDir="D:\\UpLoad_Files\\";

    //返回的是通过文件对话框选择的文件名,这之中包含了文件的目录信息
    upLoadFileName=upLoadFile.PostedFile.FileName; //去掉目录信息,返回文件名称
    upLoadFileName=Path.GetFileName(upLoadFileName); if (!Directory.Exists(upLoadFileToDir))

    //如果目录不存在,则创建一个
    Directory.CreateDirectory(upLoadFileToDir);

    //得到上传目录及文件名称
    upLoadFilePath=upLoadFileToDir+upLoadFileName;

    //获得并显示上传文件的属性

    if (upLoadFile.PostedFile.ContentLength < 4194304)
    upLoadFile.PostedFile.SaveAs(upLoadFilePath);
    else
    Console.WriteLine("上传文件大小不能超过4M!");

    //获得文件名称
    fileNameLabel.Text=upLoadFileName; //获得文件类型
    fileTypeLabel.Text=upLoadFile.PostedFile.ContentType; //获得文件长度
    fileTypeLabel.Text=upLoadFile.PostedFile.ContentLength.ToString(); RequiredFieldValidator1.Text="恭喜您,上传文件成功!";
    }
    }
    }
      

  3.   


    ================================================================CSDN 论坛助手 Ver 1.0 B0402提供下载。 改进了很多,功能完备!★  浏览帖子速度极快![建议系统使用ie5.5以上]。 ★  多种帖子实现界面。 
    ★  保存帖子到本地[html格式]★  监视您关注帖子的回复更新。
    ★  可以直接发贴、回复帖子★  采用XML接口,可以一次性显示4页帖子,同时支持自定义每次显示帖子数量。可以浏览历史记录! 
    ★  支持在线检测程序升级情况,可及时获得程序更新的信息。★★ 签名  ●  
         可以在您的每个帖子的后面自动加上一个自己设计的签名哟。Http://www.ChinaOK.net/csdn/csdn.zip
    Http://www.ChinaOK.net/csdn/csdn.rar
    Http://www.ChinaOK.net/csdn/csdn.exe    [自解压]