试试在ASPX页面中的<form..>添加:encType="multipart/form-data" 
如:
<form id="form1" action="webform1.aspx" method="post" encType="multipart/form-data" runat="server">

解决方案 »

  1.   

    在ASP时代要上传一个文件是很麻烦的事,有了ASP.Net事情就变得简单.这里我给出一个实例,希望能对朋友们有帮助.文件上传的实例, 来自http://www.chinabs.net">中国BS网<%@ Import Namespace="System.IO" %>
    <%@ page Language="C#" debug="true" %>
    <html>
    <head>
    <title>上传文件 , http://www.chinabs.net </title>
    <script language="C#" runat="server">
     //This method is called when the "upload" button id pressed
     public void UploadFile(object sender , EventArgs E)
     {
       //检查上传文件不为空
       if(myFile.PostedFile!=null)
       {     
      string nam = myFile.PostedFile.FileName ;
      //取得文件名(抱括路径)里最后一个"."的索引
      int i= nam.LastIndexOf(".");
      //取得文件扩展名
      string newext =nam.Substring(i);
      //这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
      DateTime now = DateTime.Now; 
      string newname=now.DayOfYear.ToString()+myFile.PostedFile.ContentLength.ToString(); 
      //保存文件到你所要的目录,这里是IIS根目录下的upload目录.你可以改变.
      //注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
      myFile.PostedFile.SaveAs(Server.MapPath("\\upload\\"+newname+newext)); 
      //得到这个文件的相关属性:文件名,文件类型,文件大小
      fname.Text=myFile.PostedFile.FileName;
      fenc.Text=myFile.PostedFile.ContentType ;
      fsize.Text=myFile.PostedFile.ContentLength.ToString();
       }
     }
      

  2.   

    </script>
    </head>
    <body>
    <center>
    <h3> 文件上传的实例, 来自<a href="http://www.chinabs.net">中国BS网</a></h3>
    <form id="uploderform" method="post" action="FileUpload.aspx" enctype="multipart/form-data"  runat="server" >
    <table border="1" cellspacing="2" cellpadding="2" >
    <tr> <td><h5>选择要上传的文件:</h5></td</tr>
    <tr>
    <td>
    <input type="file" id="myFile" runat="server" NAME="myFile">
    </td>
    </tr>
    <tr><td>
    <input type="button"  value="上 传" OnServerClick="UploadFile" runat="server" ID="Button1" NAME="Button1">
    </td></tr>
    </table>
    </form>
    <br>
    <br>
    <table border="1" cellspacing="2">
    <tr><td><b>文件资料</b></td>
    <td>&nbsp;</td> 
    </tr>
    <tr>
    <td>文件名 :</td>
    <td><asp:label id="fname" text="" runat="server" /></td></tr>
    <tr>
    <td>文件类型 :</td>
    <td><asp:label id="fenc" runat="server" /></td></tr>
    <tr>
    <td>文件大小 :(in bytes)</td>
    <td><asp:label id="fsize" runat="server" /></td></tr>
    </table>
    <br>
    <br>
    <br>
    </center>
    </body>
    </html>
      

  3.   

    我写的项目里边的
    试试
    private void BtnAddside_Click(object sender, System.EventArgs e)
    {
    if (contractid.Items.Count == 0 || insertimg.PostedFile.FileName.Length == 0)
    {
    return;
    }
    Stream stream = insertimg.PostedFile.InputStream;
    byte[] buffer = new byte[insertimg.PostedFile.ContentLength];
    try
    {
    if ((int)stream.Length/1024 > 2048)
    {
    ShowErrMsg("上传附件的最大尺寸为2M,请检查!");
    return;
    }
                // Read the bytes from this stream            
    int aa = stream.Read(buffer, 0, (int)stream.Length);            // Now we can close the stream            
    int size = (int)stream.Length;
    stream.Read(buffer, 0, size);
    SqlConnectionConfig();
    conn.Open();
    // Create a stored procedure command       
    SqlCommand cmd = new SqlCommand("sp_InsertSide_1", conn);        
    cmd.CommandType = CommandType.StoredProcedure;        // Add the return value parameter       
    cmd.Parameters.Add("@name", SqlDbType.Char,20).Value = System.IO.Path.GetFileNameWithoutExtension(insertimg.PostedFile.FileName.ToString()); 
    cmd.Parameters.Add("@size", SqlDbType.Int).Value = Convert.ToInt32(size/1024); 
    cmd.Parameters.Add("@side", SqlDbType.Image).Value = buffer; 
    cmd.Parameters.Add("@expandname", SqlDbType.Char).Value = Path.GetExtension(insertimg.PostedFile.FileName.ToString());              
    cmd.Parameters.Add("@contractname",SqlDbType.VarChar).Value = contractid.SelectedItem.Text.Trim();
    cmd.ExecuteNonQuery(); 
    //InsertImage(buffer,(int)stream.Length/1024);
    buffer = null;        
    stream.Close();
    ShowErrMsg("添加附件成功!");
    }
    catch(Exception ex)
    {
    ShowErrMsg("添加附件时出现错误,请重试!"+ex.ToString());
    }
    finally
    {
    conn.Close();
    }
    }
      

  4.   

    另外,补充下:要给存文件的目录授权给ASPNET用户写权限。
      

  5.   

    ASPX頁面如下:
    <%@ Page language="c#" Codebehind="upload.aspx.cs" AutoEventWireup="false" Inherits="zjlweb.WebForm2" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm2</title>
    <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="WebForm2" method="post" encType="multipart/form-data" runat="server">
    <FONT id="FONT1" face="新細明體" runat="server">
    <h2>
    <strong><font color="red">
    <center><asp:label id="hand" Runat="server" text="上傳文件示列"></asp:label></center>
    </font></strong>
    </h2>
    <hr font color="#0033ff">
    <br>
    文件名<INPUT id="UpFile" style="WIDTH: 318px; HEIGHT: 22px" type="file" size="33" name="UpFile" runat="server">
    <asp:button id="button1" Text="上傳" Runat="server" Height="23px"></asp:button><br>
    <br>
    <asp:Label ID="fname" Runat="server" />
    <br>
    <br>
    <asp:Label ID="fenc" Runat="server" />
    <br>
    <br>
    <asp:Label ID="fsize" Runat="server" />
    <br>
    <br>
    </form>
    </FONT></FONT></FORM>
    </body>
    </HTML>
    CS頁面如下
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace zjlweb
    {
    /// <summary>
    /// WebForm2 的摘要描述。
    /// </summary>
    public class WebForm2 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button button1;
    protected System.Web.UI.WebControls.Label fname;
    protected System.Web.UI.WebControls.Label fenc;
    protected System.Web.UI.WebControls.Label fsize;
    protected System.Web.UI.HtmlControls.HtmlGenericControl FONT1;
    protected System.Web.UI.WebControls.Label hand;
    protected System.Web.UI.HtmlControls.HtmlInputFile UpFile;

    private void Page_Load(object sender, System.EventArgs e)
    {
    string Second=null;
    if(DateTime.Now.Second.ToString().Length==1)
    Second="0"+DateTime.Now.Second.ToString();
    else
    Second=DateTime.Now.Second.ToString();
    Response.Write(Second); 
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 此呼叫為 ASP.NET Web Form 設計工具的必要項。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 此為設計工具支援所必需的方法 - 請勿使用程式碼編輯器修改
    /// 這個方法的內容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.button1.Click += new System.EventHandler(this.Button1_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void Button1_Click(object sender, System.EventArgs e)
    {      if(UpFile.PostedFile!=null)
    {     
    string nam = UpFile.PostedFile.FileName ;
                    //取得最后一個.的值
    int i= nam.LastIndexOf(".");
                    //取得最后一個\\的值
    int a=nam.LastIndexOf("\\");
    //取得文件名和擴展名的全稱
                    string b=nam.Substring(a+1);
    //取得文件擴展名
    string newext =nam.Substring(i);
    //取得文件名:
    string name=nam.Substring(a+1,(b.Length-newext.Length));                UpFile.PostedFile.SaveAs("e:\\Inetpub\\wwwroot\\ie\\"+name+""+newext+""); 
           
    fname.Text="文件名稱:  "+name+newext;
    fenc.Text="文件類型:   "+UpFile.PostedFile.ContentType ;
    fsize.Text="文件大小:  "+UpFile.PostedFile.ContentLength.ToString()+"位";
    }
    }
    }
    }
      

  6.   

    一下引用ynduanlian():
    —————————————————————————————————————————   另外,补充下:要给存文件的目录授权给ASPNET用户写权限。—————————————————————————————————————————如何在IIS中授权写权限呢,除了主目录中给写入权限外,还有什么要设置的呢
      

  7.   

    http://www.codeproject.com/aspnet/fileupload.asp?target=upload
      

  8.   

    各位大哥,看看我的代码再说吧
    我不要用<input type="file"..> 这个控件
    我想要的是像我的代码中那样,只获取一个地址值,就可以实现上传,地址值是可通过变量传递的