3

I have to upload an image to cloudinary with react-native. I have used this code:

uploadImage(source) {
let timestamp = (Date.now() / 1000 | 0).toString();
let api_key = '***********'
let api_secret = '**************'
let cloud = '*****'
let hash_string = 'timestamp=' + timestamp + api_secret
let signature = CryptoJS.SHA1(hash_string).toString();
let upload_url = 'https://api.cloudinary.com/v1_1/' + cloud + '/image/upload'

let xhr = new XMLHttpRequest();
xhr.open('POST', upload_url);
xhr.onload = () => {
  console.log(xhr);
};
let formdata = new FormData();
formdata.append('file', {uri: source, type: 'image/png', name: 'upload.png'});
formdata.append('timestamp', timestamp);
formdata.append('api_key', api_key);
formdata.append('signature', signature);
xhr.send(formdata);};

But I get this error:

TypeError:expected dynamic type 'string' but had type 'object'

enter image description here

4
  • Try formdata.append('file', JSON.stringify({uri: source, type: 'image/png', name: 'upload.png'})); Commented Jan 31, 2017 at 11:15
  • 1
    yup now its working but its not uploading in the cloud..! Commented Jan 31, 2017 at 12:29
  • What response code are you getting? Commented Jan 31, 2017 at 12:44
  • 1
    "error":{"message":"Unsupported source URL: {\\"uri\\":{\\"uri\\":\\"file:///storage/emulated/0/Pictures/image-0eb186ab-5d04-4037-affd-dfb49b92c7ec.jpg\\"},\\"type\\":\\"image/png\\",\\"name\\":\\"upload.png Commented Jan 31, 2017 at 13:22

1 Answer 1

3

If you got "source" from "react-native-image-picker", try to change

formdata.append('file', {uri: source, type: 'image/png', name: 'upload.png'}); 

into

 formdata.append('file', {uri: source.uri, type: 'image/png', name: 'upload.png'});

in android. I fixed my problem in this way.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.