pdf为什么不能从数据库中读取? 我写的代码成功的将word文挡和图片写入了数据库,并能读取,但在读取pdf文档时却显示文件格式被破坏,不知什么原因?请大家指教. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 “将word文挡和图片写入了数据库,并能读取,但在读取pdf文档时却显示文件格式被破坏”如果你是将数据库中的文档以二进制流的形式读取的话,那么就需要在读取文件时正确设置文档的格式(ContentType)! 数据库中保存了文档格式,图片和word都可以读取 试试 Response.Clear(); Response.ClearHeaders(); Response.Buffer = false; Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition","attachment;filename=" +HttpUtility.UrlEncode(fi.FullName,System.Text.Encoding.UTF8)); Response.AppendHeader("Content-Length",fi.Length.ToString()); Response.WriteFile(fi.FullName); Response.Flush(); Response.End(); 查到的显示pdf文件:private void showfile(string commandstr) { string filesize,filename; byte[] content; SqlConnection conn=new SqlConnection(con); SqlCommand cmd=new SqlCommand(commandstr,conn); conn.Open(); SqlDataReader sr; sr=cmd.ExecuteReader(); if(sr.Read()) { content=(byte[])sr["SC_image"]; filesize=content.Length.ToString(); filename=Common.Util.Get_filename(sr["source_file_name"].ToString()); if(sr["image_type"].ToString()=="application/pdf") filename="x.pdf"; Response.ContentType = sr["image_type"].ToString(); Response.AddHeader("Content-Length",filesize); Response.AddHeader("Content-Disposition", "inline;filename="+filename); Response.BinaryWrite(content); } sr.Close(); conn.Close(); } filename=Common.Util.Get_filename(sr["source_file_name"].ToString()); 这句什么意思? 为什么word用相同的代码可以保存和读取,而pdf读取时却显示"文件被破坏并无法修复"?郁闷啊!!! 代码稍微改一下就可以了,如下: string filesize,filename; byte[] content; SqlConnection conn = new SqlConnection("Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=billding;Data Source=127.0.0.1"); string commandstr = "select * from ImageStore where ImageId = 6 "; SqlCommand cmd=new SqlCommand(commandstr,conn); conn.Open(); SqlDataReader sr; sr=cmd.ExecuteReader(); if(sr.Read()) { content=(byte[])sr["ImageData"]; filesize=content.Length.ToString(); filename="temp.pdf"; //"application/pdf") Response.ContentType = "application/pdf";//sr["image_type"].ToString(); Response.AddHeader("Content-Length",filesize); Response.AddHeader("Content-Disposition", "inline;filename="+filename); Response.BinaryWrite(content); } sr.Close(); conn.Close(); 把这个读wis文件的程序转换为C#版本的,能出正确的结果 如何用office2007组件生成后缀为xls的Excel 提取字符串问题 如何截取锁定界面的那张图啊!! 请问:这个界面如何实现的?VS2005中的“工具”下的“选项”界面,分2部分,点左边,则右边整体会出现不同的内容。想不出来啊 数据更新问题 没有强名称 正则表达式问题 关于页面循环的问题!希望能进帮我解决!(在线等!搞好马上结!) 怎样以编程的方式找到某数据库的数据文件和日志文件的名称和所在的位置? 在window 2003 server上安装.net2003一定要把系统分区转化为ntfs格式吗 請問如何過濾掉column?
如果你是将数据库中的文档以二进制流的形式读取的话,那么就需要在读取文件时正确设置文档的格式(ContentType)!
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition","attachment;filename=" +HttpUtility.UrlEncode(fi.FullName,System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length",fi.Length.ToString());
Response.WriteFile(fi.FullName);
Response.Flush();
Response.End();
显示pdf文件:
private void showfile(string commandstr)
{
string filesize,filename;
byte[] content;
SqlConnection conn=new SqlConnection(con);
SqlCommand cmd=new SqlCommand(commandstr,conn);
conn.Open();
SqlDataReader sr;
sr=cmd.ExecuteReader();
if(sr.Read())
{
content=(byte[])sr["SC_image"];
filesize=content.Length.ToString();
filename=Common.Util.Get_filename(sr["source_file_name"].ToString());
if(sr["image_type"].ToString()=="application/pdf")
filename="x.pdf";
Response.ContentType = sr["image_type"].ToString();
Response.AddHeader("Content-Length",filesize);
Response.AddHeader("Content-Disposition", "inline;filename="+filename);
Response.BinaryWrite(content);
}
sr.Close();
conn.Close();
}
这句什么意思?
郁闷啊!!!
string filesize,filename;
byte[] content;
SqlConnection conn = new SqlConnection("Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=billding;Data Source=127.0.0.1");
string commandstr = "select * from ImageStore where ImageId = 6 ";
SqlCommand cmd=new SqlCommand(commandstr,conn);
conn.Open();
SqlDataReader sr;
sr=cmd.ExecuteReader();
if(sr.Read())
{
content=(byte[])sr["ImageData"];
filesize=content.Length.ToString();
filename="temp.pdf"; //"application/pdf")
Response.ContentType = "application/pdf";//sr["image_type"].ToString();
Response.AddHeader("Content-Length",filesize);
Response.AddHeader("Content-Disposition", "inline;filename="+filename);
Response.BinaryWrite(content);
}
sr.Close();
conn.Close();