Unable to upload image to Dropbox using Matlab code
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
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 个评论
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 Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!