Function TForm1.ACloud_UploadPicture(_PictureID, _OriPictureFPathAndName:
String) :
String;
Var
POSTData: TIdMultipartFormDataStream;
JSON: ISuperObject;
begin
Result := '
';
try
//generate a so called Multipart Object with
//- the picture File itself. it is loaded as Stream form the path specified
//- identifiers needed for the Cake server routines 'picture' , 'image/jpg'
POSTData := TIdMultiPartFormDataStream.Create;
POSTData .AddFile('
picture', _OriPictureFPathAndName, '
image/jpg');
IDHTTP.HandleRedirects := True;
IDHTTP.Request.BasicAuthentication := false;
IDHTTP.Request.Authentication := TIdBasicAuthentication.Create;
IDHTTP.Request.Authentication.Username := aCloudLogin.Username;
IDHTTP.Request.Authentication.Password := aCloudLogin.Password;
//although we handle the Multipart message, servers answers are again JSON for easier interpretation
IdHTTP.Request.ContentType := '
application/json';
aCloudURL := URLEncode(aCloudServer + '
pictures/uplpic.json?PictureID=' + _PictureID+'
.jpg');
JSONResponse := IDHTTP.Post(aCloudURL, POSTData);
except
on E :
Exception do
Result := Result + '
Exception uploading an image to aCloud: "' + E.ClassName + '
" "' + E.
Message +'
"';
end;
POSTData.Free;
//---------------------------
if Result <> '
'
then exit;
//---------------------------
//Now let's check if everythimng worked correctly. ...we interprte the Response returend from the Server
//This is specific to your servers specifications and just an example on how it could be done
JSON := SO();
JSON := SO(JSONResponse);
// Interprete the response to the JSON Super-Object (SO)
If (Uppercase(JSON.S['
response']) <> Uppercase('
OK') )
OR
(Uppercase(JSON.S['
entityID']) <> Uppercase(_PictureID+'
.jpg'))
OR
(Uppercase(JSON.S['
entityType']) <> Uppercase('
picturedata') )
OR
(Uppercase(JSON.S['
type']) <> Uppercase('
u') )
then Result := Result + '
Error responded updating an aCloud-picture: ' + JSONResponse;
end;