Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Opening word/pdf document in another window

Opening word/pdf document in another window

Scheduled Pinned Locked Moved ASP.NET
databasealgorithmshelptutorial
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    indian143
    wrote on last edited by
    #1

    Hi All, I am saving a word document in database as bytes and in another column I am saving the file name, I am providing a link button or an anchor tag for the user to select when the user clicks on it the user should be able to see the file opened in another browser window. Can you anybody please help me on it a little bit any code snippet, a link or even a suggestion would help me a lot. I am also searching but asking if somebody can guide me in a right direction. Thanks in advance.

    Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

    L B 2 Replies Last reply
    0
    • I indian143

      Hi All, I am saving a word document in database as bytes and in another column I am saving the file name, I am providing a link button or an anchor tag for the user to select when the user clicks on it the user should be able to see the file opened in another browser window. Can you anybody please help me on it a little bit any code snippet, a link or even a suggestion would help me a lot. I am also searching but asking if somebody can guide me in a right direction. Thanks in advance.

      Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Plenty of suggestions at show word file in browser - Google Search[^].

      I 1 Reply Last reply
      0
      • I indian143

        Hi All, I am saving a word document in database as bytes and in another column I am saving the file name, I am providing a link button or an anchor tag for the user to select when the user clicks on it the user should be able to see the file opened in another browser window. Can you anybody please help me on it a little bit any code snippet, a link or even a suggestion would help me a lot. I am also searching but asking if somebody can guide me in a right direction. Thanks in advance.

        Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

        B Offline
        B Offline
        Blikkies
        wrote on last edited by
        #3

        You can add target="_blank" to you hyperlink button and set the button to navigate to a generic handler that will get the file from your database and display it.

        1 Reply Last reply
        0
        • L Lost User

          Plenty of suggestions at show word file in browser - Google Search[^].

          I Offline
          I Offline
          indian143
          wrote on last edited by
          #4

          Hi I tried this way but it shows me the document in binary format

          protected void Page_Load(object sender, EventArgs e)
          {
          Investigator _objInvestigator = new Investigator();
          InvestigatorService _objinvestigatorService = new InvestigatorService();

                  int InvestigatorId;
                  InvestigatorId = ((Session\["InvestigatorId"\] != null) && int.TryParse(Session\["InvestigatorId"\].ToString(), out InvestigatorId)) ? InvestigatorId : 0;
                  \_objInvestigator = \_objinvestigatorService.GetByInvestigatorId(InvestigatorId);
                  if (\_objInvestigator != null)
                  {
                      if ((\_objInvestigator.CvFile != null) && !string.IsNullOrEmpty(\_objInvestigator.CvFileNameWithExt))
                      {
                          String ext = System.IO.Path.GetExtension(\_objInvestigator.CvFileNameWithExt);
                         
                              Response.ContentType = "application/" + ext;
                              Response.AddHeader("content-length", \_objInvestigator.CvFile.Length.ToString());
                              Response.BinaryWrite(\_objInvestigator.CvFile);
                         
                             
                      }
                  }
                  
              }  
          

          And this the code for link button click

          protected void loadFile_Click(object sender, EventArgs e)
          {
          Response.Write(string.Format("<script>window.open('{0}','_blank');</script>", "TheFile.aspx"));
          }

          Any suggestions please?

          Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

          B 1 Reply Last reply
          0
          • I indian143

            Hi I tried this way but it shows me the document in binary format

            protected void Page_Load(object sender, EventArgs e)
            {
            Investigator _objInvestigator = new Investigator();
            InvestigatorService _objinvestigatorService = new InvestigatorService();

                    int InvestigatorId;
                    InvestigatorId = ((Session\["InvestigatorId"\] != null) && int.TryParse(Session\["InvestigatorId"\].ToString(), out InvestigatorId)) ? InvestigatorId : 0;
                    \_objInvestigator = \_objinvestigatorService.GetByInvestigatorId(InvestigatorId);
                    if (\_objInvestigator != null)
                    {
                        if ((\_objInvestigator.CvFile != null) && !string.IsNullOrEmpty(\_objInvestigator.CvFileNameWithExt))
                        {
                            String ext = System.IO.Path.GetExtension(\_objInvestigator.CvFileNameWithExt);
                           
                                Response.ContentType = "application/" + ext;
                                Response.AddHeader("content-length", \_objInvestigator.CvFile.Length.ToString());
                                Response.BinaryWrite(\_objInvestigator.CvFile);
                           
                               
                        }
                    }
                    
                }  
            

            And this the code for link button click

            protected void loadFile_Click(object sender, EventArgs e)
            {
            Response.Write(string.Format("<script>window.open('{0}','_blank');</script>", "TheFile.aspx"));
            }

            Any suggestions please?

            Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

            B Offline
            B Offline
            Blikkies
            wrote on last edited by
            #5

            what is the file's extension? .doc or .docx I think it is your content type that is incorrect If the extension is .doc, the content type should be application/msword and if your extension is .docx, the content type should be application/vnd.openxmlformats-officedocument.wordprocessingml.document

            I 3 Replies Last reply
            0
            • B Blikkies

              what is the file's extension? .doc or .docx I think it is your content type that is incorrect If the extension is .doc, the content type should be application/msword and if your extension is .docx, the content type should be application/vnd.openxmlformats-officedocument.wordprocessingml.document

              I Offline
              I Offline
              indian143
              wrote on last edited by
              #6

              The content type can be three 1. doc, 2. docx and then .pdf but I think I pretty much got answer from you but if you can just give me whats the type for the pdf. Thanks buddy. :)

              Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

              1 Reply Last reply
              0
              • B Blikkies

                what is the file's extension? .doc or .docx I think it is your content type that is incorrect If the extension is .doc, the content type should be application/msword and if your extension is .docx, the content type should be application/vnd.openxmlformats-officedocument.wordprocessingml.document

                I Offline
                I Offline
                indian143
                wrote on last edited by
                #7

                Just asking is there no way that I can give a rating for your answer? If there is any I would like to give you excellent answer rating :)

                Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

                1 Reply Last reply
                0
                • B Blikkies

                  what is the file's extension? .doc or .docx I think it is your content type that is incorrect If the extension is .doc, the content type should be application/msword and if your extension is .docx, the content type should be application/vnd.openxmlformats-officedocument.wordprocessingml.document

                  I Offline
                  I Offline
                  indian143
                  wrote on last edited by
                  #8

                  Hey buddy still docx is showing binary data doesn't look good to humans man. Here is the code, can you please let me know where did I do mistake?

                  Investigator _objInvestigator = new Investigator();
                  InvestigatorService _objinvestigatorService = new InvestigatorService();

                          int InvestigatorId;
                          InvestigatorId = ((Session\["InvestigatorId"\] != null) && int.TryParse(Session\["InvestigatorId"\].ToString(), out InvestigatorId)) ? InvestigatorId : 0;
                          \_objInvestigator = \_objinvestigatorService.GetByInvestigatorId(InvestigatorId);
                          if (\_objInvestigator != null)
                          {
                              if ((\_objInvestigator.CvFile != null) && !string.IsNullOrEmpty(\_objInvestigator.CvFileNameWithExt))
                              {
                                  String ext = System.IO.Path.GetExtension(\_objInvestigator.CvFileNameWithExt);
                  
                                  //string path = Server.MapPath("~\\\\E:\\\\karthikeyan\\\\venky\\\\pdf\\\\aaaa.PDF");
                                  //WebClient client = new WebClient();
                                  //Byte\[\] buffer = client.DownloadData(path);
                                  //if (buffer != null)
                                  //{
                  
                                  if (ext.ToLower() == ".doc")
                                  {
                                      Response.ContentType = "application/" + "msword";
                                      Response.AddHeader("content-length", \_objInvestigator.CvFile.Length.ToString());
                                      Response.BinaryWrite(\_objInvestigator.CvFile);
                                  }
                                  else if(ext.ToLower() == ".docx")
                                  {
                                      Response.ContentType = "application/" + "vnd.openxmlformats-officedocument.wordprocessingml.document";
                                      Response.AddHeader("content-length", \_objInvestigator.CvFile.Length.ToString());
                                      Response.BinaryWrite(\_objInvestigator.CvFile);
                                  }
                                  else if (ext.ToLower() == ".pdf")
                                  {
                                      Response.ContentType = "application/" + ext;
                                      Response.AddHeader("content-length", \_objInvestigator.CvFile.Length.ToString());
                                      Response.BinaryWrite(\_objInvestigator.CvFile);
                                  }
                                     
                              }
                          }
                  

                  Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

                  B 1 Reply Last reply
                  0
                  • I indian143

                    Hey buddy still docx is showing binary data doesn't look good to humans man. Here is the code, can you please let me know where did I do mistake?

                    Investigator _objInvestigator = new Investigator();
                    InvestigatorService _objinvestigatorService = new InvestigatorService();

                            int InvestigatorId;
                            InvestigatorId = ((Session\["InvestigatorId"\] != null) && int.TryParse(Session\["InvestigatorId"\].ToString(), out InvestigatorId)) ? InvestigatorId : 0;
                            \_objInvestigator = \_objinvestigatorService.GetByInvestigatorId(InvestigatorId);
                            if (\_objInvestigator != null)
                            {
                                if ((\_objInvestigator.CvFile != null) && !string.IsNullOrEmpty(\_objInvestigator.CvFileNameWithExt))
                                {
                                    String ext = System.IO.Path.GetExtension(\_objInvestigator.CvFileNameWithExt);
                    
                                    //string path = Server.MapPath("~\\\\E:\\\\karthikeyan\\\\venky\\\\pdf\\\\aaaa.PDF");
                                    //WebClient client = new WebClient();
                                    //Byte\[\] buffer = client.DownloadData(path);
                                    //if (buffer != null)
                                    //{
                    
                                    if (ext.ToLower() == ".doc")
                                    {
                                        Response.ContentType = "application/" + "msword";
                                        Response.AddHeader("content-length", \_objInvestigator.CvFile.Length.ToString());
                                        Response.BinaryWrite(\_objInvestigator.CvFile);
                                    }
                                    else if(ext.ToLower() == ".docx")
                                    {
                                        Response.ContentType = "application/" + "vnd.openxmlformats-officedocument.wordprocessingml.document";
                                        Response.AddHeader("content-length", \_objInvestigator.CvFile.Length.ToString());
                                        Response.BinaryWrite(\_objInvestigator.CvFile);
                                    }
                                    else if (ext.ToLower() == ".pdf")
                                    {
                                        Response.ContentType = "application/" + ext;
                                        Response.AddHeader("content-length", \_objInvestigator.CvFile.Length.ToString());
                                        Response.BinaryWrite(\_objInvestigator.CvFile);
                                    }
                                       
                                }
                            }
                    

                    Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

                    B Offline
                    B Offline
                    Blikkies
                    wrote on last edited by
                    #9

                    Are you able to open pdf files or is it also showing binnary?

                    I 1 Reply Last reply
                    0
                    • B Blikkies

                      Are you able to open pdf files or is it also showing binnary?

                      I Offline
                      I Offline
                      indian143
                      wrote on last edited by
                      #10

                      Yes it was showing in binary but I resolved it buddy

                      Thanks, Abdul Aleem "There is already enough hatred in the world lets spread love, compassion and affection."

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups