Upload a File and Read Data with FileReader in Angular 2

后端 未结 3 1270
北荒
北荒 2020-12-14 22:47

I am trying to create an upload form in Angular 2 ts (2.2.1), that allows the upload of e.g. a CSV file, but instead of the file being sent straight to a http service I want

相关标签:
3条回答
  • 2020-12-14 23:11

    mxii's answer implicitly casts to string, which didn't work for me, maybe with the newer versions of angular it's not allowed anymore.

    what did work for me was;

    JSON.parse(fileReader.result as string)
    
    0 讨论(0)
  • 2020-12-14 23:13

    Do it like this:

    myReader.onloadend = (e) => {
       console.log(myReader.result);
       this.fileString = myReader.result;
    };
    

    So you can access your variables.

    For a more detailed explanation: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Functions/Arrow_functions

    0 讨论(0)
  • 2020-12-14 23:16

    it work, try like this...

    this.uploader.uploadAll = () => {
            console.log(this.uploader.queue.length)
            let fileCount: number = this.uploader.queue.length;
            if (fileCount > 0) {
                this.uploader.queue.forEach((val, i) => {
                    var reader = new FileReader();
                    reader.onloadend = (e) => {
                        var result = reader.result;
                        console.log(i + '/' + result)
                        this.file64.push(result)
                    };
                    reader.readAsDataURL(val._file);
                }
                );
            }
        }
    
    0 讨论(0)
提交回复
热议问题