Angular2 + Typescript FileReader范围

时间:2018-06-26 20:08:58

标签: angular typescript

我认为这是范围问题?但我尝试致电this.sendFiles(response);,它说: 无法读取未定义的属性“ sendFiles”

我不确定为什么吗?我如何在reader.onload

中称呼它

有想法吗?

  dropHandler(e) {
    e.stopPropagation();
    e.preventDefault();

    this.uploading = true;
    this.message = "uploading...";
    var response = [];
    //console.log(this._router.url);

    //Retrieve all the files from the FileList object
    //var files = e.target.files || e.dataTransfer.files;
    var files = [].slice.call(e.dataTransfer.files);
    var totalFiles = files.length;
    
    if (files.length > 0) {

      //Loop
      files.forEach(function (f) {
        var reader = new FileReader();

        let file = {
          FileName: f.name,
          FileType: f.type,
          FileValue: null,
          IsActive: true
        };

        //Get base64
        reader.onload = (event) => {
          //let contents = event.target['result'];
          //console.log(contents);

          file.FileValue = btoa(reader.result);
          response.push(file);

          //Are we done ?
          if (response.length == totalFiles) {
            console.log('allbase64');
            this.sendFiles(response);
          }
        };
        reader.readAsDataURL(f);

      });
      
    }
    
 }
    
 //
 sendFiles(files: any) {
    console.log(files.length);
    console.log(files);

    //Now loop array and send file(s) to the server!
    files.forEach(function (f) {
      console.log(f);

      //UPLOADER SERVICE
      this.uploaderService.upload(f).
        subscribe(
        res => {
          //input.value = null;
          console.log(f);
          //this.message = msg;

        }
        );

    });

  }

0 个答案:

没有答案