最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Upload and read file client side, with angular 2 - Stack Overflow

programmeradmin4浏览0评论

I need log file(s) from user so I can read and analyze those. For example somekind of drop area, where user drops a file, then I can read it with javascript?

I use Angular2 rc5. I have node.js running backside, but I don't need the data there. I need it only at client side.

Is it possible to read and parse file content with just frontend tech, like angular2 and javascript? Or do I have to upload the file to server and analyze it there?

I need log file(s) from user so I can read and analyze those. For example somekind of drop area, where user drops a file, then I can read it with javascript?

I use Angular2 rc5. I have node.js running backside, but I don't need the data there. I need it only at client side.

Is it possible to read and parse file content with just frontend tech, like angular2 and javascript? Or do I have to upload the file to server and analyze it there?

Share Improve this question asked Sep 2, 2016 at 17:18 KatuKatu 7011 gold badge8 silver badges22 bronze badges 1
  • Yes it's possible in browsers that support it stackoverflow.com/questions/16505333/… – artem Commented Sep 3, 2016 at 3:14
Add a comment  | 

1 Answer 1

Reset to default 23

It is possible!

I ended up doing it like this. This reads all the files that are selected with file dialog. I don't need to send these to node.js. I can just manipulate these on client.

<input type='file' accept='text/plain' multiple (change)='openFile($event)'>

openFile(event) {
    let input = event.target;
    for (var index = 0; index < input.files.length; index++) {
        let reader = new FileReader();
        reader.onload = () => {
            // this 'text' is the content of the file
            var text = reader.result;
        }
        reader.readAsText(input.files[index]);
    };
}

It is very basic example of how you can do it.

发布评论

评论列表(0)

  1. 暂无评论