ASP.net怎么打开数据库中以二进制存贮的WORD文档或别的文档? 我把文件以二进制的形式放到数据库中,现在我要怎么打开这个文件呀? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 SqlDataReader dr = cmd.ExecuteReader(strSql); while (dr.Read()) { ary = (byte[])dr["Content"]//假设我这里的“content”字段为二进制,,ary 为byte数组 }二进字在数据库里是image类型,你取出时你可以把image转换成byte[]数组, 我把WORD 文件写到数据库里了,现在要把它在客户端打开? Response.ContentType = attachInfo.FileType.ToString();//得到文件类型,.doc、.gif、.jpg等等Response.AppendHeader("Content-Disposition", "attachment;fileName=" + HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(attachInfo.FileName))); try { Response.BinaryWrite(----);//"----"表示你的二进制文件 } catch { Page.ClientScript.RegisterStartupScript(null, "", "<script>alert('没有找到该附件,文件可能已经丢失!')</script>"); } string strCnn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;SqlConnection myConnection = new SqlConnection(strCnn);SqlCommand command = new SqlCommand("select 附件, 附件名称, 附件类型 from 表名 Where NewsID =" + Request.QueryString["id"], myConnection);myConnection.Open();SqlDataReader dr = command.ExecuteReader();if (dr.Read()){ Response.Clear(); Response.HeaderEncoding = System.Text.Encoding.GetEncoding("GB2312"); //Response.Charset = "UTF-8"; Response.AddHeader("Content-Type", dr["附件类型"].ToString()); // File Upload时存到数据库的 Response.AppendHeader("TITLE", dr["附件名称"].ToString()); //Response.AppendHeader("Content-Disposition", "inline;filename=\"" + dr["AttachmentName"].ToString() + "\""); string fileName = dr["附件名称"].ToString(); Response.AppendHeader("Content-Disposition", "inline;filename=\"" + dr["AttachmentName"].ToString() + "\""); Response.BinaryWrite((byte[])dr["附件"]);}dr.Close();myConnection.Dispose(); string strCnn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;SqlConnection myConnection = new SqlConnection(strCnn);SqlCommand command = new SqlCommand("select 附件, 附件名称, 附件类型 from 表名 Where NewsID =" + Request.QueryString["id"], myConnection);myConnection.Open();SqlDataReader dr = command.ExecuteReader();if (dr.Read()){ Response.Clear(); Response.HeaderEncoding = System.Text.Encoding.GetEncoding("GB2312"); Response.AddHeader("Content-Type", dr["附件类型"].ToString()); // File Upload时存到数据库的 Response.AppendHeader("TITLE", dr["附件名称"].ToString()); Response.AppendHeader("Content-Disposition", "inline;filename=\"" + dr["附件名称"].ToString() + "\""); Response.BinaryWrite((byte[])dr["附件"]);}dr.Close();myConnection.Dispose(); 编码改下试试: Response.HeaderEncoding = System.Text.Encoding.GetEncoding("GB2312"); 把GB2312 改成别的. word文档是可以打开的,大家说说怎么回事? string strCnn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;SqlConnection myConnection = new SqlConnection(strCnn);SqlCommand command = new SqlCommand("select 附件, 附件名称, 附件类型 from 表名 Where NewsID =" + Request.QueryString["id"], myConnection);myConnection.Open();SqlDataReader dr = command.ExecuteReader();if (dr.Read()){ Response.Clear(); Response.HeaderEncoding = System.Text.Encoding.GetEncoding("GB2312"); Response.AddHeader("Content-Type", dr["附件类型"].ToString()); // File Upload时存到数据库的 Response.AppendHeader("TITLE", dr["附件名称"].ToString()); Response.AppendHeader("Content-Disposition", "inline;filename=\"" + dr["附件名称"].ToString() + "\""); Response.BinaryWrite((byte[])dr["附件"]);}dr.Close();myConnection.Dispose();这个可以 这个和11楼的是一样的呀,我试了DOC,JPG可以打开,但试TIF,XLS不能打开! tif打开是乱码,xls文件要下载下来才能打开,不能在IE上打开? 怎么获取存储过程的返回值? 用js怎么设置单元格的边框? 动态页面 关于内容页显示母版页的图片 ▲▲▲没有解决的iframe自适应高度问题,请大家看看。 高手帮忙看看页面传值,在线等 请问:为什么我的邮件只能存放在Queue文件夹里而不是发到邮箱中能? 关于如何做通用异常处理? 谁能逐条解释一下这段函数的意思 如何在datagrid中设置我选中得行!来者有分 fckeditor 404错误 “会话状态已创建一个会话ID,但由于响应已被应用程序刷新而无法保存它”咋回事?
while (dr.Read())
{
ary = (byte[])dr["Content"]//假设我这里的“content”字段为二进制,,ary 为byte数组
}
二进字在数据库里是image类型,你取出时你可以把image转换成byte[]数组,
Response.AppendHeader("Content-Disposition", "attachment;fileName=" + HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(attachInfo.FileName)));
try
{
Response.BinaryWrite(----);//"----"表示你的二进制文件
}
catch
{
Page.ClientScript.RegisterStartupScript(null, "", "<script>alert('没有找到该附件,文件可能已经丢失!')</script>");
}
string strCnn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;
SqlConnection myConnection = new SqlConnection(strCnn);
SqlCommand command = new SqlCommand("select 附件, 附件名称, 附件类型 from 表名 Where NewsID =" + Request.QueryString["id"], myConnection);
myConnection.Open();
SqlDataReader dr = command.ExecuteReader();
if (dr.Read())
{
Response.Clear();
Response.HeaderEncoding = System.Text.Encoding.GetEncoding("GB2312");
//Response.Charset = "UTF-8";
Response.AddHeader("Content-Type", dr["附件类型"].ToString()); // File Upload时存到数据库的
Response.AppendHeader("TITLE", dr["附件名称"].ToString());
//Response.AppendHeader("Content-Disposition", "inline;filename=\"" + dr["AttachmentName"].ToString() + "\"");
string fileName = dr["附件名称"].ToString();
Response.AppendHeader("Content-Disposition", "inline;filename=\"" + dr["AttachmentName"].ToString() + "\"");
Response.BinaryWrite((byte[])dr["附件"]);
}
dr.Close();
myConnection.Dispose();
string strCnn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;
SqlConnection myConnection = new SqlConnection(strCnn);
SqlCommand command = new SqlCommand("select 附件, 附件名称, 附件类型 from 表名 Where NewsID =" + Request.QueryString["id"], myConnection);
myConnection.Open();
SqlDataReader dr = command.ExecuteReader();
if (dr.Read())
{
Response.Clear();
Response.HeaderEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AddHeader("Content-Type", dr["附件类型"].ToString()); // File Upload时存到数据库的 Response.AppendHeader("TITLE", dr["附件名称"].ToString());
Response.AppendHeader("Content-Disposition", "inline;filename=\"" + dr["附件名称"].ToString() + "\""); Response.BinaryWrite((byte[])dr["附件"]);
}
dr.Close();
myConnection.Dispose();
把GB2312 改成别的.
SqlConnection myConnection = new SqlConnection(strCnn);
SqlCommand command = new SqlCommand("select 附件, 附件名称, 附件类型 from 表名 Where NewsID =" + Request.QueryString["id"], myConnection);
myConnection.Open();
SqlDataReader dr = command.ExecuteReader();
if (dr.Read())
{
Response.Clear();
Response.HeaderEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.AddHeader("Content-Type", dr["附件类型"].ToString()); // File Upload时存到数据库的 Response.AppendHeader("TITLE", dr["附件名称"].ToString());
Response.AppendHeader("Content-Disposition", "inline;filename=\"" + dr["附件名称"].ToString() + "\""); Response.BinaryWrite((byte[])dr["附件"]);
}
dr.Close();
myConnection.Dispose();这个可以