Unable to upload image to Dropbox using Matlab code

1 Ansicht (letzte 30 Tage)
James Chow
James Chow am 7 Aug. 2023
Kommentiert: Rik am 9 Aug. 2023
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.

Antworten (1)

Rik
Rik am 7 Aug. 2023
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 Kommentare
James Chow
James Chow am 8 Aug. 2023
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.
Rik
Rik am 9 Aug. 2023
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by