我想实现文件的上传和下载
现在已经将文件转换成byte[]保存到数据库中的image字段中了
但是下载的时候总是获取不到文件的内容比如我上传一个文本文档,里面写了几个字“123456”  转换成byte[6]存入数据库
下载时byte[] binary = (byte[])dr["FileByte"];获取出来却是byte[13] 里面的内容变成System.Byte[]
而且不管上传什么文件dll或者exe下载下来的都是byte[13]
请问什么原因??求指导!!!!
PS:由于某种原因,不能存储文件路径,所以不要叫我换成这种方式

解决方案 »

  1.   

     using (FileStream fs = new FileStream(SavePath.SelectedPath + "\\" + fileName, FileMode.Create))
                        {
                            using (BinaryWriter bw = new BinaryWriter(fs))
                            {
                                string sql = "select documentContent from document where documentID =" + Convert.ToInt32(lvContent.SelectedItems[i].SubItems[0].Text);
                                try
                                {
                                    DBHelper.conn.Open();
                                    SqlCommand cmd = new SqlCommand(sql, DBHelper.conn);
                                    cmd.CommandTimeout = 0;//设置生成文件的时间为不限制
                                    byte[] imgs = (byte[])cmd.ExecuteScalar();
                                    bw.Write(imgs);
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(ex.Message);
                                    return;
                                }
                                finally
                                {
                                    if (DBHelper.conn.State == ConnectionState.Open)
                                        DBHelper.conn.Close();
                                }
                            }
                        }
      

  2.   


            Response.ContentType = "text/plain";
                Response.AppendHeader("Content-Disposition", string.Format("attachment;filename=hello.txt"));
                Response.AppendHeader("Content-Length", binary.Length.ToString());
                Response.OutputStream.Write(binary, 0, binary.Length);
      

  3.   

     里面的内容变成System.Byte[]是你存入的时候错误导致
      

  4.   

    存的时候存错了,存成byteArray.ToString()了吧。
      

  5.   

    谢谢各位我直接用sql语句存就有问题用             sqlCommand1.Parameters.Add("@FileByte", System.Data.SqlDbType.Image);
                            sqlCommand1.Parameters[1].Value = buffByte;
    就存的就没问题