简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM