I am trying to upload an image to Dropbox using the Dropbox API and Matlab's webwrite function. I get a successful response when running the below code. However, the response says 'size: 215' when I know the size of the image should be lower than that. Furthermore, the image uploaded to Dropbox appears to be corrupted. It does not preview, and after I download and try to open the uploaded image, it does not register as a valid image. I think the issue might have something to do with the encoding, but I can't figure it out. When I use this same code to upload a plain text file with ASCII characters, it works without any problem. Does anyone know what the problem might be?
% Read the file from disk
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
catch someException
disp(someException);
throw(addCause(MException('uploadToDropbox:unableToUploadFile','Unable to upload file.'),someException));
end
RESPONSE
HTTP/1.1 200 OK
Cache-Control: no-cache
X-Content-Type-Options: nosniff
X-Server-Response-Time: 901
Content-Type: application/json
Accept-Encoding: identity,gzip
Date: Thu, 21 Apr 2022 01:25:01 GMT
Server: envoy
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
X-Robots-Tag: noindex, nofollow, noimageindex
Content-Encoding: gzip
Vary: Accept-Encoding
X-Dropbox-Response-Origin: far_remote
X-Dropbox-Request-Id: 12cee658f5604167905348fd834c35d5
Transfer-Encoding: chunked
name: 'swiss.png'
path_lower: '/swiss.png'
path_display: '/swiss.png'
id: 'id:B71uxiCfSp4AAAAAAAAAOQ'
client_modified: '2022-04-21T01:25:00Z'
server_modified: '2022-04-21T01:25:01Z'
rev: '015dd1ffa916ba20000000266082110'
size: 215
is_downloadable: 1
content_hash: 'e87f106dcb11834c94b28b84ac739413b24390fae01127db49b81dcad29c62ae'