数据库二进制文件在页面下载
private void getFileData(string file_code)
{
DataTable dt = l.getFileData(file_code);
if (dt != null && dt.Rows.Count > 0)
{
string type = dt.Rows[0]["content_type"].ToString();
byte[] b2 = (byte[])dt.Rows[0]["file_data"];
long len = Convert.ToInt64(dt.Rows[0]["file_length"]);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString("yyyy-MM-dd-HHmmssfff") + dt.Rows[0]["file_ext"].ToString());
Response.AddHeader("Content-Length ", len.ToString());
Response.ContentType = type;
Response.BinaryWrite(b2);
Response.End();
}
}
{
DataTable dt = l.getFileData(file_code);
if (dt != null && dt.Rows.Count > 0)
{
string type = dt.Rows[0]["content_type"].ToString();
byte[] b2 = (byte[])dt.Rows[0]["file_data"];
long len = Convert.ToInt64(dt.Rows[0]["file_length"]);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString("yyyy-MM-dd-HHmmssfff") + dt.Rows[0]["file_ext"].ToString());
Response.AddHeader("Content-Length ", len.ToString());
Response.ContentType = type;
Response.BinaryWrite(b2);
Response.End();
}
}
数据库存储内容:ContentType,扩展名,二进制数据。
以下是将文本文件转为二进制:
private byte[] changeFileToByte(string filename)
{
FileStream fs = File.OpenRead(filename);
byte[] fByte = new byte[fs.Length];
fs.Read(fByte, 0, fByte.Length);
fs.Close();
return fByte;
}
{
FileStream fs = File.OpenRead(filename);
byte[] fByte = new byte[fs.Length];
fs.Read(fByte, 0, fByte.Length);
fs.Close();
return fByte;
}