简体   繁体   English

使用javascript读取txt文件

[英]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.. 我需要从浏览器中读取一个文件,我不能使用ajax ..有必要在本地阅读..

this is not a duplicate from Reading a file using javascript 这不是使用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? ps:我也不能使用像V8这样的引擎http://code.google.com/p/v8/我需要用javascript的当前原生API读取它!..有什么办法可以做到这一点吗?

ps2: it must run only with chrome, or firefox! ps2:它必须只运行chrome或firefox! IE and others doesnt matter IE和其他人无关紧要

Here is the sample: DEMO 以下是样本: 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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