I am trying to open the PDF on clicking a link in browser. It is actually from DB as BLOB. But i am getting Error : file does not begin with ‘ pdf-‘ asp.net
This is the code i am using,
if (dt.Rows.Count > 0)
{
string strExtenstion = dt.Rows[0]["DGN0101FILE_EXTENSION"].ToString();
byte[] bytFile = (byte[])dt.Rows[0]["DGN0101FILE"];
string fileName = dt.Rows[0]["DGN0101FILE_NAME"].ToString();
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
if (strExtenstion == ".doc" || strExtenstion == ".docx")
{
Response.ContentType = "application/vnd.ms-word";
}
else if (strExtenstion == ".xls" || strExtenstion == ".xlsx")
{
Response.ContentType = "application/vnd.ms-excel";
}
else if (strExtenstion == ".pdf")
{
Response.ContentType = "application/pdf";
}
Response.AddHeader("Content-Length", bytFile.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.BinaryWrite(bytFile);
Response.Flush();
Response.End();
Response.Clear();
}
A valid PDF file starts with
%PDF-1.5or similar. My guess is that your data is not being stored or retrieved properly.