日期:2014-05-17  浏览次数:20751 次

从数据库中获取image字段
我想实现文件的上传和下载
现在已经将文件转换成byte[]保存到数据库中的image字段中了
但是下载的时候总是获取不到文件的内容

比如我上传一个文本文档,里面写了几个字“123456” 转换成byte[6]存入数据库
下载时byte[] binary = (byte[])dr["FileByte"];获取出来却是byte[13] 里面的内容变成System.Byte[]
而且不管上传什么文件dll或者exe下载下来的都是byte[13]


请问什么原因??求指导!!!!


PS:由于某种原因,不能存储文件路径,所以不要叫我换成这种方式


------解决方案--------------------
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();
}
}
}
------解决方案--------------------
上传的时候就传错了吧