Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: DataSnap/REST - Upload image from client to server.


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 0
André Diel

Posts: 27
Registered: 7/28/07
DataSnap/REST - Upload image from client to server.  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 8, 2015 12:03 PM
Hello,

I am having problem to trying send a file or JSON class for the server.

The return of the server is "message content is not a valid json value".
I use Delphi XE5.
Also, I'm using the RESTClient and RESTRequest components for the connection.

Below is my routine client and server.

{Call}
SendExame('C:\Users\xxx\Pictures\Image01.png');
 
{Client}
procedure SendExame(file_path: String);
var
  jsonObj: TJSONObject;
  mStream: TMemoryStream;
  aParam: TRESTRequestParameter;
begin
  jsonObj := TJSONObject.Create;
  try
    mStream := TMemoryStream.Create;
    try
      mStream.LoadFromFile(file_path);
      mStream.Position := 0;
      jsonObj.AddPair('file', IdEncoderMIME.EncodeStream(mStream));
    finally
      mStream.Free;
    end;
 
    RESTRequest1.Params.Clear;
    aParam             := RESTRequest1.Params.AddItem;
    aParam.Value       := jsonObj.ToString;
    aParam.ContentType := ctAPPLICATION_JSON;
  finally
    jsonObj.Free;
  end;
 
  RESTRequest1.method   := rmPost;
  RESTRequest1.Resource := 'UploadImage';
  RESTRequest1.Execute;
end;
 
 
{Server}
procedure TdtmServerMethods.updateUploadImage(jsonResult: TJSONObject);
var
  mStream: TMemoryStream;
begin
  mStream := TMemoryStream.Create;
  try
    TIdDecoderMIME.DecodeStream((jsonResult.Get('file').JsonValue as TJSONString).Value, mStream);
    mStream.Position := 0;
    mStream.SaveToFile('C:\Test.png');
  finally
    mStream.Free;
  end;
end;
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02