cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
If you’ve changed your email address, now's the perfect time to update it on your Dropbox account and we’re here to help! Learn more 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: 

Unable to correctly upload images using the Dropbox API in Matlab

Unable to correctly upload images using the Dropbox API in Matlab

reseda333
New member | Level 2
I am trying to upload an image to Dropbox using the Dropbox API and Matlab's webwrite function.  I get a successful response when running the below code. However, the response says 'size: 215' when I know the size of the image should be lower than that. Furthermore, the image uploaded to Dropbox appears to be corrupted. It does not preview, and after I download and try to open the uploaded image, it does not register as a valid image. I think the issue might have something to do with the encoding, but I can't figure it out. When I use this same code to upload a plain text file with ASCII characters, it works without any problem. Does anyone know what the problem might be?
 
% Read the file from disk
fid = fopen(dataFile, 'r');
data = char(fread(fid)');
fclose(fid);
 
[~,remoteFName, remoteExt] = fileparts(dataFile);
headerFields = {'Authorization', ['Bearer ', dropboxAccessToken]};
headerFields{2,1} = 'Dropbox-API-Arg';
headerFields{2,2} = sprintf('{"path": "/%s%s", "mode": "add", "autorename": true, "mute": false}',remoteFName, remoteExt);
 
headerFields{3,1} = 'Content-Type';
headerFields{3,2} = 'application/octet-stream';
 
headerFields = string(headerFields);
 
 
% Set the options for WEBWRITE
opt = weboptions;
opt.MediaType = 'application/octet-stream';
opt.CharacterEncoding = 'ISO-8859-1';
opt.RequestMethod = 'post';
opt.HeaderFields = headerFields;
 
 
% Upload the file
try
   tempOutput = webwrite('https://content.dropboxapi.com/2/files/upload', data, opt);
catch someException
   disp(someException);
   throw(addCause(MException('uploadToDropbox:unableToUploadFile','Unable to upload file.'),someException));
end
 
 

RESPONSE

HTTP/1.1 200 OK
Cache-Control: no-cache
X-Content-Type-Options: nosniff
X-Server-Response-Time: 901
Content-Type: application/json
Accept-Encoding: identity,gzip
Date: Thu, 21 Apr 2022 01:25:01 GMT
Server: envoy
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
X-Robots-Tag: noindex, nofollow, noimageindex
Content-Encoding: gzip
Vary: Accept-Encoding
X-Dropbox-Response-Origin: far_remote
X-Dropbox-Request-Id: 12cee658f5604167905348fd834c35d5
Transfer-Encoding: chunked

name: 'swiss.png'
path_lower: '/swiss.png'
path_display: '/swiss.png'
id: 'id:B71uxiCfSp4AAAAAAAAAOQ'
client_modified: '2022-04-21T01:25:00Z'
server_modified: '2022-04-21T01:25:01Z'
rev: '015dd1ffa916ba20000000266082110'
size: 215
is_downloadable: 1
content_hash: 'e87f106dcb11834c94b28b84ac739413b24390fae01127db49b81dcad29c62ae'


1 Reply 1

Greg-DB
Dropbox Staff

The Dropbox API will save whatever data you supply in the HTTPS request body for the upload call as the file data, so you'll need to make sure you're sending the exact correct binary data that you want to store.

 

In the code you shared here, that seems to be your "data" variable. Make sure that variable contains the data you expect to be sending.

 

We can't officially provide support for third party platforms like MATLAB, as that's not made by Dropbox, but looking at the rest of your code, I recommend investigating the use of the fopen, fread, and char methods. For instance, is that char method call needed/correct? It looks like it may be modifying the data you mean to send.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?