none
C# - Unable to display documents or download even stored from database. PDF is working fine RRS feed

  • Question

  • Below is the code. Please let me know where I am going wrong. Because if pdf extension works great.

    using

    System;


    using

    System.Collections.Generic;


    using

    System.Linq;


    using

    System.Web;


    using

    System.Web.UI;


    using

    System.Web.UI.WebControls;


    using

    System.Configuration;


    using

    System.Data;


    using

    System.Data.SqlClient;


    using

    System.Configuration;



    namespace

    DisplayPDF

    {

       

    publicpartialclassDisplayPPOM: System.Web.UI.Page


        {

           

    protectedvoidPage_Load(objectsender, EventArgse)

            {

               

    if(!IsPostBack)

                {

                   

    try

                    {

                       

    // sanitize query string by converting it to an int; anything else will cause an exception


                      

    intfileID = int.Parse(Request.QueryString[0]);

                       

    //int fileID = int.Parse(Request.QueryString["FileID"]);


                        View(fileID);

                    }

                   

    catch(SystemExceptionex)

                    {

                       

    stringdisplayError = ConfigurationManager.AppSettings["displayError"].ToString();

                       

    if(displayError == "1")

                        {

                            Label1.Text += ex.Message;

                        }

                       

    else


                        {

                            Label1.Text +=

    "There is no data. An error occurred";

                        }

                    }

                }

            }

           

    protectedvoidView(intfileID)

            {  

               

    //string embed = "<object id=\"obj1\" data=\"{0}{1}\" type=\"application/pdf\"  >";


               

    //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>";


                ProcessRequest2(

    HttpContext.Current);

            }

           

    publicvoidProcessRequest2(HttpContextcontext)

            {

              

    intid = int.Parse(context.Request.QueryString[0]);

               

    //int id = int.Parse(context.Request.QueryString["FileID"]);


               

    byte[] bytes;

               

    stringconstr = ConfigurationManager.ConnectionStrings["ppomConnectionString"].ConnectionString;

               

    stringFileName = "";

               

    stringfileextension;

               

    using(SqlConnectioncon = newSqlConnection(constr))

                {

                   

    using(SqlCommandcmd = newSqlCommand())

                    {

                        cmd.CommandType =

    CommandType.StoredProcedure;

                        cmd.Connection = con;

                        con.Open();

                        cmd.CommandText =

    "SELECT FileExtension FROM vsd.FileAttributes WHERE id=@FileId";

                

                        cmd.Parameters.AddWithValue(

    "@FileID", id);

                       

    //cmd.Parameters["@FileLeafRef"].Direction = ParameterDirection.Output;


                       

    //cmd.Parameters["@FileLeafRef"].Value = FileName;


                       

                        cmd.CommandText =

    "smms.GetPPOM";

                       

    using(SqlDataReadersdr2 = cmd.ExecuteReader())

                        {

                            sdr2.Read();

                            bytes = (

    byte[])sdr2["Binary"];

                            fileextension = sdr2[

    "FileExtension"].ToString();

                        }

                        con.Close();

                       

    //Label1.Text += bytes.Length.ToString() + " bytes from db <br />";


                    }

                }

               

    if(fileextension.Substring(fileextension.IndexOf('.') + 1).ToLower() == "pdf")

                {

                    context.Response.Buffer =

    true;

                    context.Response.Charset =

    "";

                    context.Response.Cache.SetCacheability(

    HttpCacheability.NoCache);

                    context.Response.ContentType =

    "application/PDF";

                    context.Response.BinaryWrite(bytes);

                    context.Response.Flush();

                    context.Response.End();

                }

               

    else


                   

    if(fileextension.Substring(fileextension.IndexOf('.') + 1).ToLower() == "doc")

                    {

                        context.Response.ContentType =

    "application/vnd.openxmlformats-officedocument.wordprocessing‌​ml.document";

                    }

                   

    elseif(fileextension.Substring(fileextension.IndexOf('.') + 1).ToLower() == "docx")

                     {

                         context.Response.ContentType =

    "application/octet-stream";

                        

    //Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";


                    }

               

    //Label1.Text += "before BinaryWrite <br />";


             

               

    // the code below was in the original prototype app with the prototype db, left in in case we need it later


               

    //if (context.Request.QueryString["download"] == "1")


               

    //{


               

    //    context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);


               

    //}


                context.Response.Cache.SetCacheability(

    HttpCacheability.NoCache);

               

    //context.Response.ContentType = "application/pdf";


              

               

    //return bytes.Length.ToString() + " bytes from db";


            }

        }

    }

    • Moved by CoolDadTx Wednesday, September 27, 2017 2:12 PM ASP.NET related
    Monday, September 25, 2017 8:18 PM

All replies

  • Hello SurbhikShah,

    According to your question is more related to ASP.NET, You could post a new thread to ASP.NET forum for suitable support . There are a lot of experience people that could help you work out the problem quickly.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools . If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution .

    Best Regards,

    neil hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 27, 2017 12:59 PM