Search code examples
javascriptimagereact-nativebase64fetch

how to upload base64 encoded png of signature to server in react native


I am new to react native. I am trying to upload the base64 encoded png to server using formData. but I am keep getting error. network request failed. please help. thanks. here is the code

this.setState({base64: `data:image/jpeg;base64,${result.encoded}`})
console.log(this.state.base64)

  formData.append('digital_signature', 
           {
             
               uri:this.state.base64,         
              // uri: this.state.singleFileADH1,
              name: 'digital_signature.jpg',
              type: 'image/*'
          }
       );

   fetch('https://abc.tech/Android_API_CI/upload_multipart_data',
      {
        method: 'post',
          body : formData,
          
        headers: {
          'Content-Type': 'multipart/form-data;',
        },
        
      }

<SignatureCapture
          style={styles.signature}
          ref="sign"
          onSaveEvent={this._onSaveEvent}
          onDragEvent={this._onDragEvent}
          showNativeButtons={false}
          // minStrokeWidth={-10}
          // saveImage={true}
          showTitleLabel={false}
          viewMode={'portrait'}
        />


Solution

  •  const file = {
          uri:
            Platform.OS == 'android'
              ? ImageUri.path
              : `file://${ImageUri.path}`,
          name: 'image.jpg',
          type: ImageUri.mime, // e.g. 'image/jpg'
        };
        formdata.append('user_photo', file);
    

    may this help you