从数据库查看PDF文档;文件不以'%PDF - '

时间:2016-03-17 18:55:32

标签: asp.net pdf

我有一个将PDF文档上传到数据库的应用。我的应用程序有一个按钮来查看PDF文档。当我单击视图按钮时,会显示一个消息框,并显示“文件不以'%PDF-'开头”。数据库中的文件是.pdf文件,我的数据行中有二进制数据。

有没有人对PDF文件没有显示的原因有任何建议?

提前致谢!

编辑:这不是一个重复的问题。我已经卸载并重新安装了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);
    }

这是我的上传:

 // 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();
            }


        }

    }

截至目前,我收到错误:Firefox:XML解析错误:找不到任何元素 地点:http://localhost:59259/FileCS.ashx?Id=0 第1行,第1列:

Internet Explorer:文件不以'%PDF'

开头

Google Chrome:没有错误,只显示空白PDF。

再次,非常感谢你的帮助!

0 个答案:

没有答案