Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 FIle API: Security Error while reading a file

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

like image 577
Claudio Albertin Avatar asked Jun 21 '11 15:06

Claudio Albertin


1 Answers

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.

like image 152
ebidel Avatar answered Oct 21 '22 10:10

ebidel