Display .htm files content in .aspx page control

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shashi shekhar singh
    New Member
    • Aug 2009
    • 30

    Display .htm files content in .aspx page control

    Dear sir,

    I have a word document file contains text and images, now i have saved it as a web page and wants to display it on browser ,
    using ,
    string str=directory.g etfiles("");
    response.conten tType="text/html";
    response .writefile("");
    it displayed in different format and images not shown
    blank spaces shown as ?????????? and image as X.

    i have no. of files stored as word file ,tried to display directly in gridview but it's not posible for me, so i converted them as web page and now i want to display 5 files dynamically in gridview including images., so help me out, looking for ur reply..

    , can u tell me how it works, i will be thanxfull to u

    i do not want to use web browser or process.start(" IExploreAsassr. exe","path") method because it displays only one file at a time , actually i want to dislay it in gridview item template, including pics.
  • NitinSawant
    Contributor
    • Oct 2007
    • 271

    #2
    well, you can use <iframe> tag

    Code:
    <iframe src ="myFilePath.html" width="100%" height="300">
      <p>iframes not supported</p>
    </iframe>

    Comment

    • NitinSawant
      Contributor
      • Oct 2007
      • 271

      #3
      in response.write( "") pass parameter for showing the iframe tag with the desired file...

      cheers,
      Nitin

      Comment

      • ssnaik84
        New Member
        • Aug 2009
        • 149

        #4
        Code:
        <html>
        <head>
            <meta http-equiv="Content-Type" content="application/doc">
        </head>
        <body>
            <iframe src="your-word-document-file.doc" width="100%" height="100%"> </iframe>
        </body>
        </html>
        try this.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Hi Shashi,

          Have you tired viewing the HTML webpages generated by Word by themselves first?

          Make sure that they display correctly before trying to use them in your asp application.

          If they are working, consider using IFrames to display the webpages in the grid.

          Comment

          • shashi shekhar singh
            New Member
            • Aug 2009
            • 30

            #6
            i am really respectfull to u all for ur reply, i want to use like this shown as below,
            Code:
                
            <div>
                    <iframe id="iframe1" runat="server" scrolling="auto"  width="100%" height="100%"> </iframe>
                    
                </div>
            Code:
              string[] getfile = Directory.GetFiles(@"D:\Shekhar Documents", "*.htm");
                        for (int i = 1; i < getfile.Length; i++)
                       {
                           HtmlControl frame = (HtmlControl)this.FindControl("iframe1");
                           //string path = Server.MapPath("1.htm");
                           frame.Attributes["src"] = getfile[i];
                       }
            i am using this but it not displayed my file content, it only displayed http url.. like "http://www.google.com" , please help me out.. i am waiting for ur reply.
            Last edited by Frinavale; Aug 25 '09, 01:03 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

            Comment

            • NitinSawant
              Contributor
              • Oct 2007
              • 271

              #7
              try displaying only single file on the page @shashi shekhar singh

              Comment

              • shashi shekhar singh
                New Member
                • Aug 2009
                • 30

                #8
                Like
                frame.Attribute s["src"] = getfile[i];
                I am giving
                frame.Attribute s["src"] = "D://shekhardocument s/1.htm";
                then it's not displaying , am new for iframe so plzgive me reply.

                Comment

                • NitinSawant
                  Contributor
                  • Oct 2007
                  • 271

                  #9
                  Originally posted by shashi shekhar singh
                  frame.Attribute s["src"] = "D://shekhardocument s/1.htm";
                  The src attribute should be internet url e.g. http://localhost/shekhardocuments/1.htm

                  and also you should add the html files in the project directory..

                  Comment

                  • Frinavale
                    Recognized Expert Expert
                    • Oct 2006
                    • 9749

                    #10
                    Originally posted by shashi shekhar singh
                    Like
                    frame.Attribute s["src"] = getfile[i];
                    I am giving
                    frame.Attribute s["src"] = "D://shekhardocument s/1.htm";
                    then it's not displaying , am new for iframe so plzgive me reply.
                    Ok, that wont work, your 1.htm file has to be located on the web server. That way web browsers can down load it using the URL specified as the src attribute.

                    You can't use a local file path to the 1.htm file...you have to use something like "http://localhost/mywebsite/statichtm/1.htm".

                    So, place all of the htm files into a folder located within your project. Then loop through the directory and create the URL based on the files you find there.

                    So you would have something like this to create the URLs:

                    Code:
                    string[] getfile = Directory.GetFiles(Server.MapPath("~/statichtm/"), "*.htm");
                    List<string> staticHtmUrls= new List<string>();
                    
                    for (int i = 1; i < getfile.Length; i++)
                    {     string staticURL = "/statichtm/"+getfile[i];
                          staticHtmUrls.Add(staticURL );       
                    }
                    So now you have a list of URLs to your static htm files.

                    I'm thinking that it will probably be easier for you to use a Repeater instead of a GridView.

                    So your ASP code would look like:

                    Code:
                    <asp:Repeater ID="staticWebPagesRepeater" runat="server">
                            <HeaderTemplate>
                                 <table border="1">
                                    <tr>
                                       <td><b>Web Pages</b></td>
                                    </tr>
                              </HeaderTemplate>
                              <ItemTemplate>
                                 <tr>
                                    <td> <iframe src="<%# Container.DataItem %>"></iframe></td>
                                 </tr>
                              </ItemTemplate>
                              <FooterTemplate>
                                 </table>
                              </FooterTemplate>
                    </asp:Repeater>
                    And your C# code would have something like this:
                    Code:
                    private List<string> _repSource;
                    protected void Page_Load(Object sender, System.EventArgs e) 
                    {
                            _repSource =  new List<string>();
                            string fileNames[] = Directory.GetFiles(Server.MapPath("~/StaticHTM/"), "*.htm");
                            foreach (string name in fileNames)
                            {
                                string sp[] = name.Split('\');
                                string urlToStaticHTML = sp(sp.Length - 1);
                                _repSource.Add("/StaticHTM/" + urlToStaticHTML );
                            }
                            staticWebPagesRepeater.DataSource = _repSource.ToArray();
                            staticWebPagesRepeater.DataBind();
                    }

                    Comment

                    • shashi shekhar singh
                      New Member
                      • Aug 2009
                      • 30

                      #11
                      Respected Sir @Frinavale
                      I have used as u have explained by using repeater with iframe and the url , but i am geting error ...
                      C# Code:
                      Code:
                      //string[] getfile = Directory.GetFiles(Server.MapPath("~/statichtm/"), "*.htm");
                              string[] getfile = Directory.GetFiles(@"D:\Shekhar Documents\Tutorial + Development\html\Question_Html","*.htm");
                             List<string> staticHtmUrls = new List<string>();
                      
                              for (int i = 1; i < getfile.Length; i++)
                              {
                                  string staticURL = "/statichtm/" + getfile[i];
                                  staticHtmUrls.Add(staticURL);
                              }
                              staticWebPagesRepeater.DataSource = staticHtmUrls.ToArray();
                              staticWebPagesRepeater.DataBind();
                      --------------------------------------------------------------------------------
                      --------------------------------------------------------------------------------
                      Code:
                      <asp:Repeater ID="staticWebPagesRepeater" runat="server"> 
                              <HeaderTemplate> 
                                   <table border="1"> 
                                      <tr> 
                                         <td><b>Web Pages</b></td> 
                                      </tr> 
                                </HeaderTemplate> 
                                <ItemTemplate> 
                                   <tr> 
                                      <td> <iframe src="<%# Container.DataItem %></td>"></iframe></td> 
                                   </tr> 
                                </ItemTemplate> 
                                <FooterTemplate> 
                                   </table> 
                                </FooterTemplate> 
                      </asp:Repeater> 
                      
                          </div>
                      Error is:
                      Server Error in '/bytes' Application.
                      --------------------------------------------------------------------------------

                      HTTP Error 400 - Bad Request.

                      --------------------------------------------------------------------------------
                      Version Information: ASP.NET Development Server 9.0.0.0
                      one thing that i forgot to explain to you. My original source file was of word Document type(MS word 2007) (text + image), i converted them in to .htm file, so when i add all files(.htm+imag e folders) into Visual Studio Solution Explorer and open to view in browser the error is:
                      Server Error in '/bytes' Application.
                      --------------------------------------------------------------------------------

                      HTTP Error 403 - Forbidden.

                      --------------------------------------------------------------------------------
                      Version Information: ASP.NET Development Server 9.0.0.0
                      looking for your reply it's urgent for me..
                      Last edited by Frinavale; Aug 27 '09, 01:01 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

                      Comment

                      • Frinavale
                        Recognized Expert Expert
                        • Oct 2006
                        • 9749

                        #12
                        Well there are a couple of reasons why this wont work.

                        I didn't fully test the code because I was just quickly giving you an example and thought that you'd be able to figure it out from there. I work mainly in VB.NET and so I didn't test the loop that you had in your code already...needl ess to say I did not test the code, it was just an example.

                        The first problem with the code I posted is that I accidentally left a "<td>" in the src attribute in the ASP code....the ASP code should be:
                        Code:
                        <asp:Repeater ID="staticWebPagesRepeater" runat="server">
                                <HeaderTemplate>
                                     <table border="1">
                                        <tr>
                                           <td><b>Web Pages</b></td>
                                        </tr>
                                  </HeaderTemplate>
                                  <ItemTemplate>
                                     <tr>
                                        <td> <iframe src="<%# Container.DataItem %>"></iframe></td>
                                     </tr>
                                  </ItemTemplate>
                                  <FooterTemplate>
                                     </table>
                                  </FooterTemplate>
                        </asp:Repeater>
                        The second thing that is wrong with this code is that when you use the Directory.GetFi les() method it returns you a list of Path And File Names that match the pattern you're searching for.

                        We stated earlier that you cannot have a local path to a file because the web browser will not be able to download it...web browsers cannot access local files on your computer, it can only access files that exist on the web server and can only do so using a URL. You need to create a URL using the file name.

                        Because we simply prepended the "/statichtm/" to the file name, and didn't properly extract the name of the file from the Path/FileName your Urls are going to look like:

                        "/statichtm/D:\PatToVisualS tudioProject\1. htm"

                        Obviously this is not a valid URL....what you really want is just:

                        "/statichtm/1.htm"

                        This would have been very obvious if you had used the debugger and stepped through the code.

                        So what you need to do is fix the ASP code so that the "<td>" is not part of the iframe's src attribute, and modify your loop so that it extracts the file name and then properly create the URLs by prepending "/statichtm/" to the file name extracted.

                        This is the working VB.NET version of what your code should look like.
                        Code:
                        Private _repSource As List(Of String)
                        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                                _repSource = New List(Of String)
                                Dim fileNames() As String = Directory.GetFiles(Server.MapPath("~/StaticHTM/"), "*.htm")
                                For Each name In fileNames
                                    Dim sp() As String = name.Split("\"c)
                                    Dim urlToStaticHTML As String = sp(sp.Length - 1)
                                    _repSource.Add("/StaticHTM/" + urlToStaticHTML)
                                Next
                                staticWebPagesRepeater.DataSource = _repSource.ToArray
                                staticWebPagesRepeater.DataBind()
                        End Sub

                        C# would look something like (the following is obviously not tested):
                        Code:
                        private List<string> _repSource;
                        protected void Page_Load(Object sender, System.EventArgs e) 
                        {
                                _repSource =  new List<string>();
                                string fileNames[] = Directory.GetFiles(Server.MapPath("~/StaticHTM/"), "*.htm");
                                foreach (string name in fileNames)
                                {
                                    string sp[] = name.Split('\');
                                    string urlToStaticHTML = sp(sp.Length - 1);
                                    _repSource.Add("/StaticHTM/" + urlToStaticHTML );
                                }
                                staticWebPagesRepeater.DataSource = _repSource.ToArray();
                                staticWebPagesRepeater.DataBind();
                        }
                        In the future, please try to debug the problem before automatically asking for help.

                        Comment

                        • shashi shekhar singh
                          New Member
                          • Aug 2009
                          • 30

                          #13
                          Originally posted by Frinavale
                          Well there are a couple of reasons why this wont work.

                          I didn't fully test the code because I was just quickly giving you an example and thought that you'd be able to figure it out from there. I work mainly in VB.NET and so I didn't test the loop that you had in your code already...needl ess to say I did not test the code, it was just an example.

                          The first problem with the code I posted is that I accidentally left a "<td>" in the src attribute in the ASP code....the ASP code should be:
                          Code:
                          <asp:Repeater ID="staticWebPagesRepeater" runat="server">
                                  <HeaderTemplate>
                                       <table border="1">
                                          <tr>
                                             <td><b>Web Pages</b></td>
                                          </tr>
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                       <tr>
                                          <td> <iframe src="<%# Container.DataItem %>"></iframe></td>
                                       </tr>
                                    </ItemTemplate>
                                    <FooterTemplate>
                                       </table>
                                    </FooterTemplate>
                          </asp:Repeater>
                          The second thing that is wrong with this code is that when you use the Directory.GetFi les() method it returns you a list of Path And File Names that match the pattern you're searching for.

                          We stated earlier that you cannot have a local path to a file because the web browser will not be able to download it...web browsers cannot access local files on your computer, it can only access files that exist on the web server and can only do so using a URL. You need to create a URL using the file name.

                          Because we simply prepended the "/statichtm/" to the file name, and didn't properly extract the name of the file from the Path/FileName your Urls are going to look like:

                          "/statichtm/D:\PatToVisualS tudioProject\1. htm"

                          Obviously this is not a valid URL....what you really want is just:

                          "/statichtm/1.htm"

                          This would have been very obvious if you had used the debugger and stepped through the code.

                          So what you need to do is fix the ASP code so that the "<td>" is not part of the iframe's src attribute, and modify your loop so that it extracts the file name and then properly create the URLs by prepending "/statichtm/" to the file name extracted.

                          This is the working VB.NET version of what your code should look like.
                          Code:
                          Private _repSource As List(Of String)
                          Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                                  _repSource = New List(Of String)
                                  Dim fileNames() As String = Directory.GetFiles(Server.MapPath("~/StaticHTM/"), "*.htm")
                                  For Each name In fileNames
                                      Dim sp() As String = name.Split("\"c)
                                      Dim urlToStaticHTML As String = sp(sp.Length - 1)
                                      _repSource.Add("/StaticHTM/" + urlToStaticHTML)
                                  Next
                                  staticWebPagesRepeater.DataSource = _repSource.ToArray
                                  staticWebPagesRepeater.DataBind()
                          End Sub

                          C# would look something like (the following is obviously not tested):
                          Code:
                          private List<string> _repSource;
                          protected void Page_Load(Object sender, System.EventArgs e) 
                          {
                                  _repSource =  new List<string>();
                                  string fileNames[] = Directory.GetFiles(Server.MapPath("~/StaticHTM/"), "*.htm");
                                  foreach (string name in fileNames)
                                  {
                                      string sp[] = name.Split('\');
                                      string urlToStaticHTML = sp(sp.Length - 1);
                                      _repSource.Add("/StaticHTM/" + urlToStaticHTML );
                                  }
                                  staticWebPagesRepeater.DataSource = _repSource.ToArray();
                                  staticWebPagesRepeater.DataBind();
                          }
                          In the future, please try to debug the problem before automatically asking for help.

                          Thank you very much Sir,
                          I have gotted what i had to do.
                          thanks a lot again....
                          u have given a nice answer and made a nice conversetion to me. i really respect u..
                          bye.. have a nice day

                          Comment

                          • shashi shekhar singh
                            New Member
                            • Aug 2009
                            • 30

                            #14
                            Respected Sir,
                            thanx a lot for your help now i am facing some other problem . Now i have to display these selected files into gridview/repeater 5 files per page and want to make a custom next and previous button so that when user click next button it's selected radio button item inserted into database and next remaining files will be displayed on the gridview/ repeater template.
                            Looking for ur reply..

                            Comment

                            • Frinavale
                              Recognized Expert Expert
                              • Oct 2006
                              • 9749

                              #15
                              Hi Shashi,

                              Please take the time to research the problem before you post your question. The experts here are more than willing to help you with a specific problem but you have to do your part to learn the basics.

                              Once you've tried to solve the problem and you need some help, ask for help on your specific problem.

                              Comment

                              Working...