Upload and read file client side, with angular 2

后端 未结 1 1936
礼貌的吻别
礼貌的吻别 2021-02-15 22:10

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 Angul

相关标签:
1条回答
  • 2021-02-15 22:54

    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 讨论(0)
提交回复
热议问题