Asp.net 从数据库中查看PDF文档;文件不以';开头%PDF-';
我有一个将PDF文档上传到数据库的应用程序。我的应用程序有一个查看PDF文档的按钮。当我单击“查看”按钮时,会显示一个消息框,并显示“文件不是以“%PDF-”开头的”。数据库中的文件是.pdf文件,我的数据行中有二进制数据 有人对PDF文件不显示的原因有什么建议吗 提前谢谢Asp.net 从数据库中查看PDF文档;文件不以';开头%PDF-';,asp.net,pdf,Asp.net,Pdf,我有一个将PDF文档上传到数据库的应用程序。我的应用程序有一个查看PDF文档的按钮。当我单击“查看”按钮时,会显示一个消息框,并显示“文件不是以“%PDF-”开头的”。数据库中的文件是.pdf文件,我的数据行中有二进制数据 有人对PDF文件不显示的原因有什么建议吗 提前谢谢 编辑:这不是重复的问题。我已经卸载并重新安装了Adobe Reader,还有最新版本的Internet Explorer 以下是我的ashx处理程序的源代码: public class FileCS : IHttpHandl
编辑:这不是重复的问题。我已经卸载并重新安装了Adobe Reader,还有最新版本的Internet Explorer 以下是我的ashx处理程序的源代码:
public class FileCS : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
// string id = context.Request.QueryString["id"];
int id = int.Parse(context.Request.QueryString["id"]);
//var id = ((LinkButton)sender).CommandArgument;
byte[] bytes;
string fileName, contentType;
//fileName = context.Server.MapPath("~/" + context.Request.QueryString["id"]);
//masterConnectionString or PALMConnectionString6 - > change to use function GetConnection();
string constr = ConfigurationManager.ConnectionStrings["masterConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT Name, Data, ContentType FROM tblFiles1 WHERE id = @id";
cmd.Parameters.AddWithValue("@id", id);
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
sdr.Read();
// bytes = (byte[])sdr["Data"];
//contentType = sdr["SupportDoc"].ToString();
//fileName = sdr["Name"].ToString();
//com.Parameters.AddWithValue("@SupportDoc", filename1);
//com.Parameters.AddWithValue("@Name", type);
//com.Parameters.AddWithValue("@Data", bytes);
bytes = (byte[])sdr["Data"];
contentType = sdr["ContentType"].ToString();
fileName = sdr["Name"].ToString();
//bytes = (byte[])sdr["Attachment"];
//contentType = sdr["Name"].ToString();
//fileName = sdr["Type"].ToString();
context.Response.Clear();
context.Response.ClearHeaders();
context.Response.ClearContent();
context.Response.Buffer = true;
context.Response.Charset = "";
//if (context.Request.QueryString["download"] == "1")
//{
context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
context.Response.AddHeader("Content-Length", bytes.Length.ToString());
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.ContentType = "application/pdf";
context.Response.BinaryWrite(bytes);
context.Response.Flush();
context.Response.Close();
context.Response.End();
// }
// context.Response.End();
}
//con.Close();
}
con.Close();
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
这是我的链接按钮视图:
//View
protected void Button2_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(Request.QueryString["id"]);
// width = 500px
string embed = "<object data=\"{0}{1}\" type=\"application/pdf\" width=\"100%\" height=\"600px\">";
embed += "If you are unable to view file, you can download from <a href = \"{0}{1}&download=1\">here</a>";
embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
embed += "</object>";
ltEmbed.Text = string.Format(embed, ResolveUrl("~/FileCS.ashx?Id="), id);
}
到目前为止,我得到了一个错误:Firefox:XML解析错误:找不到元素
地点:
第1行第1列:
Internet Explorer:文件不是以“%PDF”开头的
谷歌浏览器:没有错误,只是显示一个空白的PDF文件
再次感谢你的帮助 我已经卸载并重新安装了Adobe,并且我有最新版本的Internet Explorer。很可能您的代码中有一个bug。如果没有看到您的代码在数据库中存储PDF或从数据库中检索PDF,我们只能猜测。如果您觉得这不是一个重复的问题,那么您应该提供更多信息来澄清这一点。例如,应用程序源代码的相关部分。好的,我对这里涉及的技术不是100%了解,但代码乍看起来还行。您能否使用
wget
或类似工具请求PDF并将响应写入文件?您能在PDF查看器中打开此文件吗?
// upload
protected void Button1_Click(object sender, EventArgs e)
{
string filename1 = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
SqlConnection con = Connection.GetConnection();
SqlCommand com = new SqlCommand();
com.CommandText = "UploadDoc";
com.CommandType = CommandType.StoredProcedure;
com.Connection = con;
com.Parameters.AddWithValue("@Name", filename1);
com.Parameters.AddWithValue("@ContentType", contentType);
com.Parameters.AddWithValue("@Data", bytes);
con.Open();
com.ExecuteNonQuery();
Label1.ForeColor = System.Drawing.Color.Green;
Label1.Text = "File Uploaded Successfully!";
con.Close();
}
}
}