Uploading an image using the Dropbox API

조회 수: 14 (최근 30일)
Ja M
Ja M 2022년 4월 23일
댓글: Rik 2022년 4월 23일
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
tempOutput = webwrite('https://content.dropboxapi.com/2/files/upload', data, opt);
catch someException
disp(someException);
throw(addCause(MException('uploadToDropbox:unableToUploadFile','Unable to upload file.'),someException));
end
Output:
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'
  댓글 수: 1
Rik
Rik 2022년 4월 23일
Maybe the problem is because you have cast your data to char instead of uint8.
I also noticed that the type of the response is JSON. I don't know if that implies the input is interpreted as JSON, or that the response itself was a JSON string.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Web Services에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by