I have an app that uploads PDF documents to a database. My application has a button to view PDF documents. When I click on the view button, a message box displays and says "File does not begin with '%PDF-'". The files that are in the database are .pdf files and there is binary data in my Data row.
Does anyone have any suggestions as to why the PDF file is not displaying?
Thanks in advance!
EDIT: This is not a duplicate question. I have uninstalled and reinstalled Adobe Reader, also I have the latest version of Internet Explorer.
Here is my source code for my ashx handler:
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;
}
}
}
This is for my linkbutton View:
//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);
}
And this is my Upload:
// 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();
}
}
}
As of right now, I am getting the error: Firefox: XML Parsing Error: no element found Location: http://localhost:59259/FileCS.ashx?Id=0 Line Number 1, Column 1:
Internet Explorer: File does not begin with '%PDF'
And Google Chrome: no errors, just a blank PDF is displaying.
Again, thank you so much for your help!