12

Problem solved, read comment

The third problem I have with the HTML5 File API: I still use Chrome 12 on Mac OS X Snow Leopard and I'm still trying to read files with the HTML5 File API, but FileHandler.error() get called because a "SECURITY_ERR" occurres. The file I try to read is a regular .txt file from my desktop, but it neither works with other files although I can open them with regular applications.

function FileHandler(files, action) {
    console.log('FileHandler called.');

    this.files = files;
    this.reader = new FileReader();
    this.action = action;

    this.handle = function() {
        console.log('FileHandler.handle called.');

        for (var i = 0; i < this.files.length; i++) {
            this.reader.readAsDataURL(files[i]);
        }
    }

    this.upload = function() {
        console.log('FileHandler.upload called.');
        console.log(this.reader);

        data = {
            content: this.reader.result
        }

        console.log(data);
    }

    this.error = function() {
        console.log('An error occurred while reading the file.');
        console.log(this.reader.error);
    }

    this.reader.onload = this.upload.bind(this);
    this.reader.onerror = this.error.bind(this);
}

The code generates the following console output: http://cl.ly/1x1o2F0l2m3L1T2c0H0i

Claudio Albertin
  • 2,076
  • 4
  • 18
  • 20
  • Problem solved! I just uploaded the script to a webspace it works perfect. – Claudio Albertin Jun 21 '11 at 15:33
  • 3
    If you're testing an app from `file://`, you can run Chrome with the following flags: `--allow-file-access-from-files --allow-file-access`. This should only be used for testing purposes. – ebidel Jun 23 '11 at 22:46
  • @ebidel You should add that comment as an answer so this post can be closed :) – Matthew Aug 31 '11 at 16:49

1 Answers1

13

If you're testing an app from file://, you can run Chrome with the following flags: --allow-file-access-from-files --allow-file-access. This should only be used for testing purposes.

ebidel
  • 23,921
  • 3
  • 63
  • 76
  • So there's no way to use a file reader on a website basically ? You can't require your users to start their chrome browser with a special flag :S – sebpiq Jul 03 '12 at 13:29
  • No `file://` protocol will only be used when testing the website locally without any server running, once deployed the file will be transfered over `http://` protocol – Valentin Jacquemin Jul 03 '12 at 20:59
  • If a tablet executes a HTML5 application, will it be using a file:// or http:// protocol? And if it is the latter, will it runs into this error too? – Extrakun Aug 22 '12 at 08:37
  • 1
    +1 for the file/http hint. But I created a Chrome shortcut and added mentioned flags as parameters and it doesn't work. How should I use those flags? – kolenda Nov 30 '12 at 15:35