Einzelnen Beitrag anzeigen

MartinK

Registriert seit: 21. Jun 2009
Ort: Germering (Germany)
89 Beiträge
 
Delphi 10.2 Tokyo Enterprise
 
#19

AW: Daten abholen von einem CakePHP Server

  Alt 8. Jan 2015, 10:47
Zwecks Vollständigekit halber hier ein weiteres CodeFragment, da ich denke das es evtl. weiteren Personen hilft die mal etwas ähnliches machen wollen

Ziel: Ein FileUpload auf den Server mittels eines sog "Multipart Form DataStreams". So etwas wird zB zum Uploaden von Bildern eingesetzt

lG Martin

Delphi-Quellcode:
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;
Martin Kuhn
  Mit Zitat antworten Zitat