to access a text file on HTTP server from browser using a script
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: to access a text file on HTTP server from browser using a script

  1. #1
    Join Date
    Jul 2007
    Posts
    1

    to access a text file on HTTP server from browser using a script

    Hi,
    i'm coding a HTTP server in C which is an embedded web server to be run on the OMAP board. on a default page when this server is accessed i'm giving an option of information about the processes running on the OMAP board which has other DSP applications running on it other than the web server. This information, i'm storing in a text file on the server side. How to write a script to send this text file from the server so, that on the browser the file contents are displayed.
    Last edited by pnayaka; July 9th, 2007 at 01:09 AM. Reason: correction

  2. #2
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,860

    Re: to access a text file on HTTP server from browser using a script

    AJAX is not a language, it is merely a manner in passing information between the server and the client using JavaScript and XML. This being said, you can't code in AJAX to do this. You need a server-side language (PHP, ASP.NET, JSP, etc.) to read the textfile and then output the data. JavaScript will then receive this text as the response text. Here is an example with PHP as the server-side language.

    whatever.html
    Code:
    <html>
    <body>
    
    <script type="text/javascript">
    var AJAX = {
      initialize: function(){
        var xmlHTTP;
        try{xmlHTTP = new XMLHttpRequest();}
        catch(e){
          try{xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");}
          catch(e){
            try{xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");}
            catch(e){
              alert("Your browser does not support AJAX!");
              return false;
            }
          }
        }
        return xmlHTTP;
      },
    
      get: function(url, func){
        var obj = this.initialize();
        obj.open('GET', url, true);
        obj.send(null);
        obj.onreadystatechange = function(){
          if(obj.readyState==4){
            func(obj.responseText);
          }
        }
      }
    }
    
    function parseFile(retText){
      document.getElementById('theDiv').innerHTML = retText;
    }
    </script>
    
    <input type="button" value="Get File" onclick="AJAX.get('readfile.php', parseFile)">
    <div id="theDiv"></div>
    
    </body>
    </html>
    readfile.php
    PHP Code:
    <?php
    $file 
    'filename.txt';
    $content = @file_get_contents($file);
    if(
    $content){
      echo 
    $content;
    }
    ?>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center