cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: Upload image file using fetch in js

Upload image file using fetch in js

Rosemol
Explorer | Level 3

Hi All,
I'm new to Dropbox API and I'm trying to upload image files from our website to dropbox using api. I'm using FETCH to POST data to dropbox and the file is uploading.However, the image is not getting loaded in dropbox. Below is code that I use:

fetch('https://content.dropboxapi.com/2/files/upload', {
    method: 'post',
    body: file1, //Base64 
    headers: {'Authorization': 'Bearer '+MY_TOKEN,
              'Content-Type': 'application/octet-stream',
              'Accept': 'application/json',
	      'Dropbox-API-Arg': '{"path": "/test1/test.jpg","mode": "add","autorename": false,"mute": false,"strict_conflict": false}'
                                }
  }).then(function(response) {
        console.log(response.json());
  });

Please help me out with this issue. Thank you

5 Replies 5

Greg-DB
Dropbox Staff

Can you clarify what you mean when you say "the image is not getting loaded in dropbox"? What are you seeing or not seeing exactly?

Also, can you share the response you get from /2/files/upload? If the call was successful, it should either contain the metadata of the uploaded file, or if it wasn't successful, an error indicating why.

Rosemol
Explorer | Level 3

Hi Greg,

after the upload action I could see the data which i'm sending to dropbox in base64 format, but instead of seeing the image I see the data which i send.

I tried different methods and for 1 option I was successful which was sending the file object which I get from <input type="file"> value. I understand that there's no need to convert the file to base64

Greg-DB
Dropbox Staff

Thanks for following up. I'm glad to hear you got this working as desired.

Yes, that's correct, there's no need to apply an additional encoding. Dropbox will accept whatever data you upload exactly as you upload it. So, if you want to upload an image that can be viewed later, you should just upload the unmodified raw data.

khan_sikki
New member | Level 2

i am getting this as response 

Error in call to API function "files/upload": HTTP header "Dropbox-API-Arg": could not decode input as JSON

Greg-DB
Dropbox Staff

@khan_sikki I see you also requested help for this issue in your support ticket and included more information there, so we'll follow up with you there shortly.

Need more support?