http://www.csdn.net/expert/topic/1053/1053203.xml?temp=.5867731

解决方案 »

  1.   

    字段为image类型即可,源代码如下:.aspx页面<%@ Page language="c#" Codebehind="file.aspx.cs" AutoEventWireup="false" Inherits="website.other.file" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>file</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="file" method="post" enctype="multipart/form-data" runat="server">
    <asp:TextBox id="user" style="Z-INDEX: 101; LEFT: 120px; POSITION: absolute; TOP: 142px" runat="server"></asp:TextBox>
    <INPUT style="Z-INDEX: 102; LEFT: 120px; POSITION: absolute; TOP: 186px" type="file" id="my_file" runat="server">
    <asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 140px; POSITION: absolute; TOP: 248px" runat="server" Text="Button"></asp:Button>
    </form>
    <br>
    <br>
    <a href="download.aspx?id=12">download</a>
    </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;
    using System.Data.SqlClient;
    using System.IO;namespace website.other
    {
    /// <summary>
    /// file 的摘要说明。
    /// </summary>
    public class file : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.TextBox user;
    protected System.Web.UI.HtmlControls.HtmlInputFile my_file;
    protected System.Web.UI.WebControls.Button Button1;
        SqlConnection cn;
    private void Page_Load(System.Object sender, System.EventArgs e)
    {
    String strConn=(String)Application["strConn"];
    cn=new SqlConnection(strConn);
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    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(System.Object sender, System.EventArgs e)
    {
    if (my_file.PostedFile!=null)
    {
    String strConn=(String)Application["strConn"];
    cn=new SqlConnection(strConn);
    HttpPostedFile myfile=my_file.PostedFile;
    int FileSize=myfile.ContentLength;
    string FileName=myfile.FileName.ToString();
    string FileType=myfile.ContentType.ToString();
    string FileUser=user.Text;
    Byte[] FileData=new Byte[FileSize];
    Stream str=my_file.PostedFile.InputStream;
    str.Read(FileData,0,FileSize);
    String strSQL="insert into upload(FileUseraspnet,FileSizeaspnet,FileNameaspnet,FileTypeaspnet,FileDataaspnet) values (@FileUser,@FileSize,@FileName,@FileType,@FileData)"; cn.Open();
    SqlCommand cm=new SqlCommand(strSQL,cn);
    cm.Parameters.Add(new SqlParameter("@FileUser",SqlDbType.VarChar)).Value=FileUser;
    cm.Parameters.Add(new SqlParameter("@FileSize",SqlDbType.Int)).Value=FileSize;
    cm.Parameters.Add(new SqlParameter("@FileName",SqlDbType.VarChar)).Value=FileName;
    cm.Parameters.Add(new SqlParameter("@FileType",SqlDbType.VarChar)).Value=FileType;
    cm.Parameters.Add(new SqlParameter("@FileData",SqlDbType.Image)).Value=FileData; try
    {
    cm.ExecuteNonQuery();
    Response.Write ("<script language=javascript>alert('上传到数据库成功!');parent.location.href='file.aspx'</"+"script>");
    }
    catch
    {
    Response.Write ("<script language=javascript>alert('不能成功!');</"+"script>");
    }
    cm.Connection.Close();
    }
    }
    }
    }
    ----------------------------------------------------------------下载的页面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;
    using System.Data.SqlClient;
    using System.Configuration;
    namespace website.other
    {
    /// <summary>
    /// download 的摘要说明。
    /// </summary>
    public class download : System.Web.UI.Page
    {
    SqlConnection cn;
    private void Page_Load(object sender, System.EventArgs e)
    {
    string strconn=Application["strConn"].ToString();
    cn=new SqlConnection(strconn);
        //string str1=ConfigurationSettings.AppSettings["dsn"];
    //取得配置文件中appsettings段的值
    string sql="select * from upload where Id="+Request["id"];
    cn.Open();
    SqlCommand cm=new SqlCommand(sql,cn);
      SqlDataReader dr;
    dr=cm.ExecuteReader();
    if (dr.Read())
    {
    Response.ContentType=dr["FileTypeaspnet"].ToString();
    //Response.ContentType="application/octet-stream";
    Response.AddHeader("Content-Disposition", "Attachment; FileName="+HttpUtility.UrlEncode(dr["FileNameaspnet"].ToString().Replace('+',' ')));
    Response.BinaryWrite((Byte[])dr["FileDataaspnet"]);
    }
    else
    {
    Response.Write("下载没有成功");
    }
    cn.Close();
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion
    }
    }