日期:2014-05-18  浏览次数:20477 次

二进制流数据保存问题
请问,我现在可以实现将数据库中的二进制流图片读取后显示在IE上,但是如何将其作为一个文件保存到本地硬盘呢?我的代码如下,请高手指点如何修改,万分感谢!
C# code

protected void Button2_Click(object sender, EventArgs e)
    {
        //建立数据库链接
        SqlConnection Con = new SqlConnection();
        Con.ConnectionString = "Data Source=5C4B31A8922E479\\WZ;" + "Database=KGEManage;" + "Integrated Security=True";
        String SqlCmd = "SELECT * FROM ImageStore WHERE ImageID = 5";
        SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
        Con.Open();
        SqlDataReader SqlReader = CmdObj.ExecuteReader();
        SqlReader.Read();
        Response.ContentType = (string)SqlReader["ImageContentType"];//设定输出文件类型
         //输出图象文件二进制数制
         Response.OutputStream.Write((byte[])SqlReader["ImageData"], 0, (int)SqlReader["ImageSize"]);
        Response.End();
        Con.Close();            
    }



这个是针对图片文件,那如果我是其他的文件,比如doc文件,是否可以用相同方法实现二进制流的存储于读取?

------解决方案--------------------
C# code

  //获取二进制数据流
        Stream data = File.Open(Server.MapPath(@"~/binary/c.doc"), FileMode.Open);
        BinaryReader reader = new BinaryReader(data);
        byte[] bytes = new byte[data.Length];
        reader.Read(bytes, 0, bytes.Length);
        data.Close();
        reader.Close();
        //
        Response.Clear();
        Response.ContentType = "application/msword";
        Response.BinaryWrite(bytes);

------解决方案--------------------
C# code

protected void Button2_Click(object sender, EventArgs e)
    {
        //建立数据库链接
        using(SqlConnection Con = new SqlConnection())
        {
            Con.ConnectionString = "Data Source=5C4B31A8922E479\\WZ;" + "Database=KGEManage;" + "Integrated Security=True";
            String SqlCmd = "SELECT * FROM ImageStore WHERE ImageID = 5";
            SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
            Con.Open();
            SqlDataReader SqlReader = CmdObj.ExecuteReader();
            SqlReader.Read();
            
            //Response.ContentType = (string)SqlReader["ImageContentType"];//设定输出文件类型
            ////输出图象文件二进制数制
            // Response.OutputStream.Write((byte[])SqlReader["ImageData"], 0, (int)SqlReader["ImageSize"]);
            
            /**/
            byte[] bytes = (byte[])SqlReader["ImageData"];
            
            
            using(FileStream fs = File.Create(Server.MapPath("~/file.jpg")))
            {
                fs.Write(bytes,0,bytes.Length);
            }
            /**/
            
            Con.Close();
        }
    }

------解决方案--------------------
C# code

 FileStream fileStream = new FileStream(filePath, FileMode.Open);
                long fileSize = fileStream.Length;
                bytes = new byte[(int)fileSize];
                fileStream.Read(bytes, 0, (int)fileSize);
                fileStream.Close();
                Response.Clear();
                Response.BinaryWrite(bytes);
                Response.End();

------解决方案--------------------
doc最好保存服务器硬盘上,不要往数据库里存储,二进流很容易产生格式错误,如果必须存数据库的话输出和输入最好转化成字符串格式用ntext字段保存