简体   繁体   中英

Read txt file using javascript

I need to read a file from the browser and I CANNOT use ajax.. it is necessary to be read locally..

this is not a duplicate from Reading a file using javascript

how can I do that?

ps: I also CANNOT use an engine like V8 http://code.google.com/p/v8/ I need to read it with the current native API from javascript!.. is there any way to do that?

ps2: it must run only with chrome, or firefox! IE and others doesnt matter

Here is the sample: DEMO

 function readMultipleFiles(evt) {
    //Retrieve all the files from the FileList object
    var files = evt.target.files;

    if (files) {
        for (var i = 0, f; f = files[i]; i++) {
            var r = new FileReader();
            r.onload = (function (f) {
                return function (e) {
                    var contents = e.target.result;
                    alert(contents);
                };
            })(f);
            r.readAsText(f);
        }
    } else {
        alert("Failed to load files");
    }
}
document.getElementById('fileinput').addEventListener('change', readMultipleFiles, false);

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