Upload Image from Expo to NestJS Server as Binary

88 views Asked by At

I have an Expo Client trying to upload images from iOS and Android to a NestJS Server.

I get a 400 error that File is required, and when I try to log the body on the server it's empty.

My client code is

        try {
          const token = await getItemAsync(ACCESS_TOKEN_KEY);
          const res = await FileSystem.uploadAsync(
            `${FULL_URL}`,
            media.assets[0].uri,
            {
              headers: {
                Authorization: `Bearer ${token}`,
              },
              fieldName: 'photo',
            }
          );
          console.log(res);
        } catch (error) {
          console.log("ERROR")
          console.log(error);
        }

And my Server on the receiving end is

    @Post()
    @UseInterceptors(FileInterceptor('photo'))
    public async createGalleryImages(
        @Req() { userId },
        @UploadedFile(new ParseFilePipe({
            validators: [new FileTypeValidator({
                fileType: 'image'
            })]
        })) file: Express.Multer.File) {
        try {
            console.log(file)
          
            return Builder<CreateGalleryImageResponse>()
                .build();
        } catch (error) {
            this.logger.error("Error creating gallery images", error, { galleryId });
        }
    }

Why isn't the expo-file-system uploadAsync sending the binary data from the file URI? I'd prefer not to use Base64 since that format would be transmitting more data than is necessary.

Any help is greatly appreciated!

0

There are 0 answers