3
votes

I am trying to figure this out with axios. I have made the direct api call with superagent and now want to know how to use with axios as the rest of my project is with axios. I know there is cloudinary-react, but this is the way I prefer to do it. Here is what I have so far.

import React, { Component } from 'react';
import Dropzone from 'react-dropzone';
import sha1 from 'sha1';
import superagent from 'superagent';
import axios from 'axios';

class Images extends Component {
  uploadFile(files) {
    console.log('uploadFile: ');
    const image = files[0];

    const cloudName = 'tbaustin';
    const url = `https://api.cloudinary.com/v1_1/${cloudName}/image/upload`;

    const timestamp = Date.now()/1000;
    const uploadPreset = 'cnh7rzwp';

    const paramsStr = `timestamp=${timestamp}&upload_preset=${uploadPreset}secret`;

    const signature = sha1(paramsStr);
    const params = {
      'api_key': 'api_key',
      'timestamp': timestamp,
      'upload_preset': uploadPreset,
      'signature': signature
    }

    let uploadRequest = superagent.post(url)
    uploadRequest.attach('file', image);

    Object.keys(params).forEach((key) => {
      uploadRequest.field(key, params[key]);
    });

    uploadRequest.end((err, res) => {
      if(err) {
        alert(err);
        return
      }

      console.log('UPLOAD COMLETE: '+JSON.stringify(res.body));
    });



//AXIOS CONTENT HERE
    // let request = axios.post(url, {file: image});
    // request.then((response) => {
    //   Object.keys(params).forEach((key) => {
    //     uploadRequest.field(key, params[key]);
    //   });
    //   console.log('UPLOAD COMPLETE: '+JSON.stringify(response.body));
    // }).catch((err) => { alert(err); });
  }

  render() {
    return (
      <div>
        <Dropzone onDrop={this.uploadFile.bind(this)}/>
      </div>
    )
  }
}

export default Images;
1
First of all the api keys and image upload logic should reside on your back end server.In my case it was Node js.You keep all your keys server side and then via axios call your node server and tell it the file to upload and node then calls cloudinary api to upload the asset. cloudinary.com/documentation/… - VivekN
I understand this is not the correct way to do it, I just wanted to know hot to make the correct axios code to make this work, that is all... - user7366497
So the correct approach here would be using axios to call your node api which will be responsible for uploading the file. It will be a post call consisting of all the form data, now a form can contain a file or any text field. - VivekN
Can you share the error message? - Maor.G
@Maor.G It was a bad request. I was not attaching the fields or file correctly is what I can best guess. - user7366497

1 Answers

1
votes

This worked for me.

    let formData = new FormData();
    formData.append("api_key",'');
    formData.append("file", image);
    formData.append("public_id", "sample_image");
    formData.append("timestamp", timeStamp);
    formData.append("upload_preset", uploadPreset);

    axios
    .post(url, formData)
    .then((result) => {
        console.log(result);
    })
    .catch((err) => {
        console.log(err);
    })