Unable to upload image to Dropbox using Matlab code

I'm using this code to upload an image to Dropbox. However, the image is corrupted when it is sent to dropbox.
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', dataFile, opt);
catch someException
disp(someException);
throw(addCause(MException('uploadToDropbox:unableToUploadFile','Unable to upload file.'),someException));
end
I have already set Dropbox Access Token and dataFile to the values I want to use.

Answers (1)

Fun fact: you actually don't use the data variable anywhere in this code. This would have been obvious if you had put this code in a function instead of a script, since the Matlab linter will give you a warning (the orange squiggle under the variable name).
Did you check the file size on dropbox? Was it indeed very small? I would expect it to be about the size of the filename.
I suspect this line will fix the problem, but without your API key I can't check. There might still be some issues with the exact data format, since char in Matlab is UTF-16, not uint8.
tempOutput = webwrite('https://content.dropboxapi.com/2/files/upload', data, opt);

4 Comments

@Rik I used this modified code and included the access token. However, it still uploads a corrupted image to my dropbox folder. Here is the access token if you want to take a look.
dropboxAccessToken = sl.Bjssc_nXAdKnGps77mLluuXo98dN9dapPOmMKdm65qFOg0buq3iXobzkUUU697Jl3BvjappgL8gUKDfqJX9FKWWJqlknoVh6tFD9YnGfSWfktGGRFx9aXp5b2Tr_-z3LbEDih9oi5Q6Y1-rVpUpDHcA;
dataFile = uigetfile({'*.jpg'},"Open file");
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);
end
How certain are you that the data is loaded the way it should be? If you want to upload the file as an octet stream, why are you using char instead on uint8?
I doubt it is a good idea to publicly share your token, nor does it do me any good, since I don't have your login credentials to compare an input and output file. That is up to you. If you want help, you will have to share an example input and output file.
How did you decide this way of reading and writing the file would work? Are you following some tutorial or guide?
I will be using this input file:
I found an online guide using this code from this forumer (https://www.mathworks.com/matlabcentral/answers/1703485-uploading-an-image-using-the-dropbox-api) and I tried modifying it but it hasn't worked for me. Also, how would I use uint8? Would I replace char with uint8 or would I need to type it somewhere else.
I'm glad to see my initial suspicion is still the same.
Anyway, the solution is to ask fread to give you a uint8. If you read the documentation for this function, you might get the idea to try this:
data = fread(fid,inf,'uint8=>uint8');
As I said, I have never tried this, and without you attaching (instead of inserting) the source and result files, it is tricky to debug this for you.

Sign in to comment.

Tags

Asked:

on 7 Aug 2023

Commented:

Rik
on 9 Aug 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!