0

I've been going over this for hours now, looking through different options on the web trying to understand -and not just replicate- how to load a text file. I couldn't get to work ANY of the examples I found, that is, until I changed from chromium to firefox. As an example, the code in stackoverflow question: HTML5 File api, reading in an xml/text file and displaying it on the page? which I write here for simplicity:

<!DOCTYPE html> 
<html> 
<head> 
<title>reading xml</title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
</head>
<body>
<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>

<script>
  function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

    // Loop through the FileList
    for (var i = 0, f; f = files[i]; i++) {

      var reader = new FileReader();

      // Closure to capture the file information.
      reader.onload = (function(theFile) {
        return function(e) {
          // Print the contents of the file
          var span = document.createElement('span');                    
          span.innerHTML = ['<p>',e.target.result,'</p>'].join('');
          document.getElementById('list').insertBefore(span, null);
        };
      })(f);

      // Read in the file
      reader.readAsText(f);
      // reader.readAsDataURL(f);
    }
  }

  document.getElementById('files').addEventListener('change', handleFileSelect, false);
  </script>
</body>

works great on firefox. I cannot get it to work in chromium. What am I missing?! thanks.

I'm using Chromium 18.0.1025.168 (Developer Build 134367 Linux) Ubuntu 11.10

Community
  • 1
  • 1
cauchi
  • 1,463
  • 2
  • 17
  • 45
  • Works for me in Chrome 22 and Chromium 15 with whatever I had to hand (simple todo text file for the former, and JSON for the latter). (http://jsfiddle.net/EYrdE/). Have you tried a different text file? Maybe you've lost something in the translation. Have you checked the console for errors? – net.uk.sweet Oct 11 '12 at 14:14

2 Answers2

0

Could it be this?

for (var i = 0, f; f = files[i]; i++) {

Shouldn't your second part be a conditional. Something like i < files.length?

Raymond Camden
  • 10,661
  • 3
  • 34
  • 68
  • I'm new at javascript, so left the original code as it was. Changed that line to `code for (var i = 0; i < files.length; i++) {` and after defined `var f = files[i];`, which to me makes more sense. Still, works nicely on firefox, not on chromium... I guess is something related to my box and not web-based? – cauchi Oct 11 '12 at 14:27
  • Maybe so - like net.uk.sweet said though - did you see anything in Console? – Raymond Camden Oct 11 '12 at 15:25
  • the console is empty. No errors given by the chromium developer's tools. I was going through something related to the use of --allow-file-access-from-files in case you try to read local files. Apparently in some versions of chromium this was an issue. But have not been able to find a sound response to the problem. – cauchi Oct 11 '12 at 15:30
0

For security reasons, to load local files I have to call chromium from the command line using:

    chromium-browser --allow-file-access-from-files

ATENTION: you have to close all chromium windows for this to take effect.

Just lost about four hours because of this...

cauchi
  • 1,463
  • 2
  • 17
  • 45