0

I have created an app which captures images and uploads to aws3. Previously, captured photos are stored in the Pictures folder and I was uploading from the Picture folder. Now, I am storing all the photos in my app folder(I have created a folder in the android device). Therefore, it is giving the above error. I mentioned contentType as 'image/jpg' in AWS config variable. But, this issue do not get resolved.

My code is,

import { RNS3 } from 'react-native-aws3';
import RNFS from 'react-native-fs';
import RNFetchBlob from 'react-native-fetch-blob';

     takePic = () => {

            const options = {
              quality: 1.0,
              maxWidth: 100,
              maxHeight: 100,
              base64: true,
              skipProcessing: true
          }

          // create a path you want to write to,(folder is already there)
              var pictureFolder = RNFetchBlob.fs.dirs.SDCardDir+'/School';

            ImagePicker.launchCamera(options,(responce)=>{

                this.state.testImage.push({ uri: responce.uri });
                  const file ={
                    uri   : responce.uri,
                    name : responce.fileName,
                    method: 'POST',
                    path : responce.path,
                    type :  responce.type,
                    notification: {
                        enabled: true
                      }
                  }

                  RNFetchBlob.fs.exists(pictureFolder).then((exists)=>{
                   if(exists){

                            RNFS.moveFile(responce.uri, `${pictureFolder}/${file.name}`)
                            .then(() => RNFS.scanFile(`${pictureFolder}/${file.name}`));
                            console.log("**************exists*******************");

                        }
                    })

                    this.setState(prevState => {
                        // get the previous state values for the arrays
                        let saveImages = prevState.saveImages;
                        // add the values to the arrays like before
                        saveImages.push(`${pictureFolder}/${file.name}`);
                        // return the new state
                        return {
                         saveImages
                        }
                  });

                    const base64 = RNFS.writeFile(responce.uri, responce.data);
                    return base64;

            });

        }

      _upload=()=>{
            if(this.state.connection_Status==="Online"){
              const config ={
                  keyPrefix :aws_keyPrefix,
                  bucket : 'Testing/',
                  region :aws_region,
                  accessKey:aws_accessKey,
                  secretKey :aws_secretKey,
                  successActionStatus :201,
                  contentType:'image/jpg'
                }

                //store captured images in an array
                this.state.saveImages.map((image) => {
                     RNS3.put(image,config)
                    .then((responce) => {
                      Alert.alert("Successfully, uploaded");
                    });
                });

}

Previously, I directly storing the file details as below,

 saveImages.push(file);

I was working fine. But, now I need to upload them from other folder so I am facing this problem.

Can anybody assist me to resolve this?

Vidya Kabber
  • 171
  • 1
  • 16

2 Answers2

0

I was getting this error because the file type was undefined. Actually, the type of file should not be undefined/null. I think passing the contentType in the config doesn't matter.

kartik tyagi
  • 6,256
  • 2
  • 14
  • 31
0

try using "type: image/jpg" or "type: image/png" instead of "content-type"

Uzef Shaikh
  • 636
  • 6
  • 11