0

I am trying to read a text file from my computer to a website. I have it working in IE, but I can't seem to make it work in Chrome. I'm not skilled in html really at all, so any assistance would be great!

<html>
<body>

    <div id = "content">
    </div>

    <script lang = "javascript">

    if (window.XMLHttpRequest)
    {
        xhttp=new XMLHttpRequest();
    }
    else // Internet Explorer 5/6
    { 
        xhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhttp.open("GET","./Test.txt",false);
    xhttp.send("");
    xmlDoc=xhttp.responseText;

    document.getElementById('content').innerHTML = xmlDoc;
    </script>

</body>
</html> 
rgdigi
  • 1,701
  • 2
  • 21
  • 31
Joshua Volearix
  • 313
  • 1
  • 2
  • 11
  • 3
    Can't make it work in Chrome implies you at least got some sort of an error. It would be useful if you posted it. – N.B. Jan 15 '13 at 16:33
  • 3
    you're trying to upload a file from the computer the browser's running to a server? that's not possible to do automatically for security reasons. – Marc B Jan 15 '13 at 16:33
  • 1
    Press F12 in Chrome and click 'console'. It should show you any javascript errors – rgdigi Jan 15 '13 at 16:33
  • This could help you I think http://stackoverflow.com/questions/371875/local-file-access-with-javascript – Ragnarokkr Jan 15 '13 at 16:34
  • No error, it just doesn't show up. IE, the text comes right up, Chrome, it's just a blank screen. – Joshua Volearix Jan 15 '13 at 16:34
  • Are you running on the file system? aka c:\foo.html – epascarello Jan 15 '13 at 16:34
  • `Test.txt` is located on your server, right? – Viktor S. Jan 15 '13 at 16:34
  • This is the error: XMLHttpRequest cannot load file:///C:/Documents%20and%20Settings/j/Desktop/Test/Test.txt. Cross origin requests are only supported for HTTP. Uncaught Error: NETWORK_ERR: XMLHttpRequest Exception 101 – Joshua Volearix Jan 15 '13 at 16:34
  • Yes, Test.txt is on the server and pulls the info in IE – Joshua Volearix Jan 15 '13 at 16:35
  • How is it on the "server" if you are reading it via file://? – epascarello Jan 15 '13 at 16:36
  • What is an URL to your HTML file? Does it starts with file:// or with http://? – Viktor S. Jan 15 '13 at 16:38
  • 1
    Yeah, as epascarello says, the error shows that the script is looking for a file in file:///C:/Documents%20and%20Settings/j/Desktop/Test/Test.txt - which is not allowed in Chrome – rgdigi Jan 15 '13 at 16:38
  • Duplicates: [Cross origin requests are only supported for HTTP but it's not cross-domain](http://stackoverflow.com/questions/8449716/cross-origin-requests-are-only-supported-for-http-but-its-not-cross-domain) and [“Cross origin requests are only supported for HTTP.” error, but I'm loading a completely local file](http://stackoverflow.com/questions/10752055/cross-origin-requests-are-only-supported-for-http-error-but-im-loading-a-co) – apsillers Jan 15 '13 at 16:49

1 Answers1

0

Do it with the Javascript File API example here

JEY
  • 6,973
  • 1
  • 36
  • 51