UPhttp://expert.csdn.net/Expert/topic/1153/1153814.xml?temp=.3446619

解决方案 »

  1.   

    等一下,我是用vb的的,我用c#看看
      

  2.   

    用SqlDataReader的GetBytes读到数组里再
    Response.BinaryWrite
      

  3.   

    楼上的说的也对啊,我要走了,这是vb的代码,你自己看吧,不行的话,明天在说
            Dim ImgID = Request.QueryString("ImgID")
            Dim con As SqlClient.SqlConnection
            '查询sql
            Dim str = Context.GetConfig("appSettings")("dsn")
            con = New SqlConnection(str)
            Dim SqlCmd = "SELECT * from 多文档信息表 where 文档编号='" & ImgID & "'"
                con.Open()
                Dim ada As New SqlClient.SqlDataAdapter(SqlCmd, con)
                Dim ds As New DataSet()
                ada.Fill(ds, "wendang")            '设定输出文件类型
                'Response.AddHeader("Content-Disposition", "attachment; filename=" & ds.Tables(0).Rows(0).Item("文件名称"))
                Response.ContentType = Trim(ds.Tables(0).Rows(0).Item("文档类型"))
                '输出图象文件二进制数制
                Dim FileByteArray() As Byte = ds.Tables(0).Rows(0).Item("文档内容")
                Dim s = ds.Tables(0).Rows(0).Item("文档内容")
                s = FileByteArray.Length
                Response.OutputStream.Write(ds.Tables(0).Rows(0).Item("文档内容"), 0, CInt(s))
                con.Close()
      

  4.   

    string DBcon=@"server=localhost;integrated security=yes;database=test_00";

    // Response.ContentType="Application/x-msexcel";Response.ContentType="Application/msword";this.Response.Clear();
    Handler.SqlBlobToFile(Session["file"].ToString(),DBcon,this.Response.OutputStream);
    this.Response.End();
    下面是SqlBlobToFile函数的实现:
    ////////////////////////////////////////////////////////////////////////////
    public static bool SqlBlobToFile(string fileName,string ConString, System.IO.Stream fs)
    {

    bool bresult=true; try
    {
    int fileDataCol = 0; 
    SqlConnection cn=new SqlConnection(ConString);
    SqlCommand cmd=new SqlCommand(@"SELECT fileData FROM imagefile WHERE filename='"+fileName +@"'",cn);

    cn.Open(); SqlDataReader dr=cmd.ExecuteReader();
    dr.Read();

    Byte[] b = new Byte[(dr.GetBytes(fileDataCol, 0, null, 0, int.MaxValue))];
    dr.GetBytes(fileDataCol, 0, b, 0, b.Length);
    dr.Close();

    cn.Close(); fs.Write(b,0,b.Length);
    fs.Close();
    } catch(SqlException ex)
    {
    bresult=false;
    } return bresult;}
      

  5.   

    改为byte[]试试,应该不会错的。
      

  6.   

    coldljy(凤舞N天) 的方法不错Response.AppendHeader("Content-Disposition","attachment; filename="  + yourfilename);
    Response.BinaryWrite(byte[]...);
    Response.End();这样来下载不会出错。
      

  7.   

    这样搞定:
    Byte[] b=new Byte[dbRead.GetBytes(2,0,null,0,int.MaxValue)];
    dbRead.GetBytes(2,0,b,0,b.Length);
    Response.BinaryWrite(b);  
    或者:
    Byte[] b=new Byte[dbRead.GetBytes(2,0,null,0,int.MaxValue)];
    dbRead.GetBytes(2,0,b,0,b.Length);
    Response.OutputStream.Write(b,0,b.Length);