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 Center および File ExchangeData Distribution Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by