asp.net 我从数据库中(不是文件夹)下载文件,下载后内容为System.Byte[],求帮助
代码如下:
SqlCommand mySelect = new SqlCommand("select * FROM files where id = 1", sqlConn);
SqlDataReader reader;
reader = mySelect.ExecuteReader();
if (reader.Read())
{
string sFileName = reader["FileName"].ToString();
String length = reader["FileSize"].ToString();
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentEncoding = Encoding.Default;
Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(sFileName, System.Text.Encoding.UTF8).Replace("+", ""));
Response.AppendHeader("Content-Length", length);
Response.ContentType = "application/octet-stream";
byte[] bt = (Byte[])reader["FileData"];
Response.OutputStream.Write(bt, 0, bt.Length);
Response.Flush();
Response.OutputStream.Close();
Response.End();
}
------解决方案--------------------------------------------------------
有必要吗?你不考虑下效率的吗?
还是建议数据库只存文件路径,那样不论你添加人家下载读取数据都要快。
更重要你代码可以少写N多!
------解决方案--------------------------------------------------------
byte[] bt = (Byte[])reader["FileData"];
---------------------
应该就是这个转换出错了。
------解决方案--------------------------------------------------------