Unable to upload image to Dropbox using Matlab code

조회 수: 1 (최근 30일)
James Chow
James Chow 2023년 8월 7일
댓글: Rik 2023년 8월 9일
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.

답변 (1개)

Rik
Rik 2023년 8월 7일
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
James Chow
James Chow 2023년 8월 8일
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 2023년 8월 9일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by