1

In my Angular app, while uploading a video, first I have to get the signed Url and then I have to do the post call to this signed url, including the video as payload. And I was doing like that:

uploadImage(bucket: BucketTypes, file: File, customUrl?: string): Observable<IUploadedImage> {
    const formData = new FormData();
    formData.append('file', file);
    let params = {
      entity: bucket,
      fileExtension: file.type
    };
    this.http.get<IUploadedImage>(
      customUrl ? customUrl : assetsUrl.uploadImage(this.baseUrl, bucket),
      {params}
    ).subscribe((response: any)=> {
      let url = response.signedUrl;
      return this.http.post<IUploadedImage>(url, formData);
    });
  }

Later on, I want to subscribe to the uploadImage function. But it is not working.

How can I do that? Can someone please explain? What have I done wrong here?

1
  • Can you please tell me if my answer worked? Commented Dec 31, 2022 at 12:42

1 Answer 1

1

I suggest to use the switchMap-operator to concatenate your http-requests and to remove the subscription inside the uploadImage()-method:

uploadImage(bucket: BucketTypes, file: File, customUrl?: string): Observable<IUploadedImage> {
    const formData = new FormData();
    formData.append('file', file);
    let params = {
        entity: bucket,
        fileExtension: file.type
    };
    return this.http.get<IUploadedImage>(
        customUrl ? customUrl : assetsUrl.uploadImage(this.baseUrl, bucket),
        {params}
    ).pipe(
        switchMap((response: any) => {
            let url = response.signedUrl;
            return this.http.post<IUploadedImage>(url, formData);
        })
    )
}

Finally, you can just subscribe to the observable returned by the uploadImage()-method:

this.uploadImage(bucket, file, customUrl).subscribe(
    (result: IUploadedImage) => {
        console.log('Result: ', result);
    }
) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.