Skip to content Skip to sidebar Skip to footer

Using Filetransfer Plugin Pass Data To Next Request In For Loop

I have a image upload Request with a param. I want to send response from 1st request to all next requests inside for loop. Page.page.ts //function to take Photos takePhoto()

Solution 1:

Solved it by Converting my UploadFileMedia() from Promise to Observable in my camera.service.ts

UploadFileMedia(path: string, url: string, fileType: string, post_id): Observable<any> {
    constfileTransfer: FileTransferObject = this.transfer.create();
    letoptions: FileUploadOptions = {
      fileKey: "images",
      fileName: fileType,
      chunkedMode: false,
      params: { post_id: post_id }
    };
    returnnewObservable(result => {
      fileTransfer.upload(path, url, options).then((data) => {
        let res = JSON.parse(data.response);            
        result.next(res);    
      }, err => {
        result.error(err);
      });
    });
  }

and inside my page.ts:

takePhoto = async () => {
    this.camera.takePhoto().then(async (res: any) => {
      for (let v = 0; v < res.length; v++) {
        awaitnewPromise((resolve, reject) => {
          this.camera.UploadFileMedia(res[v].fullPath, 'UploadImage', res[v].fullPath.split('/').pop(), this.postId).subscribe((res: any) => {
            console.log(res);
            if (res.status == true) {
              this.postId = res.data.post_id;
              this.userImages.push({ img: res.data});
              resolve(true)
            } else {                  
              reject();
            }
          })
        })
      }
    })
  }

Idea taken From:

Another Answer for Same Problem

Post a Comment for "Using Filetransfer Plugin Pass Data To Next Request In For Loop"