日期:2014-05-18 浏览次数:20560 次
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();
}
//获取二进制数据流
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);
------解决方案--------------------
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();
}
}
------解决方案--------------------
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字段保存