Watch, Follow, &
Connect with Us

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


Welcome, Guest
Guest Settings
Help

Thread: Ability to upload data and files (images) via webservices



Permlink Replies: 6 - Last Post: Jul 11, 2016 1:06 PM Last Post By: David Cox
steven chesser

Posts: 401
Registered: 4/13/09
Ability to upload data and files (images) via webservices
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 5, 2016 9:07 AM
Since I deal with a database engine no one supports, i have to write a middle man.

We have a mobile person using whatever he's gonna be using for iOS and Android (ala, not delphi)

I need to write a middle man software for him to be able to send up packet of data (part name, description, some other little details) and image files taken on the phone/tablet.

What would be better to expose this so anyone not using delphi can hit the service up?

Soap ? Rest API ?

Any examples then of attachment of said data and files?

Did some searching.. found OLD stuff that doesnt work for both cases.

I have a short window on this so need to figure out something quick.

I've played with both some but not in much detail or with file attachments.

Using 10.1 Berlin to do this in..

Edited by: steven chesser on Jul 5, 2016 11:28 AM
steven chesser

Posts: 401
Registered: 4/13/09
Re: Ability to upload data a files (image) via webservices
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 5, 2016 11:27 AM   in response to: steven chesser in response to: steven chesser
Using the REST api, I can get something to kinda work...

but two issues

1) My URL requires spaces?

http://localhost:8080/datasnap/rest/TServerMethods1/%22UploadImage%22/

I have to put %22 around my name... no idea why. Found this out by using the Chrome Inspector. Without them, I can't call any of my routines.

2) I am trying to get a small client app to work but can't. Fails the JSON part.

function UploadImage(jsonResult: TJSONObject):string;

I can only get this to work if i use the web interface deal to pass a BASE64 encoded image such as

{FileData:"blahblahblahbase64codestuff"}

I preprocess an image to base64... sent it over.. and it saved and image was good

But when I try to make my own little test client program, the server blows up with

"Message content is not a valid JSON value."

Can't figure out how to use Tidhttp to post properly to http://localhost:8080/datasnap/rest/TServerMethods1/%22UploadImage%22/
with my data

steven chesser

Posts: 401
Registered: 4/13/09
Re: Ability to upload data a files (image) via webservices
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 5, 2016 12:06 PM   in response to: steven chesser in response to: steven chesser
I still havent figured out why i need %22 for double quotes around my procedure name.

BUT

I have it finally uploading. Was screwing up the JSON.

So other than %22 if anyone has an idea, the proof of concept is working at least!
steven chesser

Posts: 401
Registered: 4/13/09
Re: Ability to upload data a files (image) via webservices
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 5, 2016 7:00 PM   in response to: steven chesser in response to: steven chesser
Appears if routine is "complex" .... it puts double quotes around it..

I made another routine that just accepted a string and returned a string. And Double quotes no needed.

So can live with that I guess.

I guess last thing I'd like to figure out if anyone knows, is how to easily convert a TDataSet to JSON?

Have a routine to kick off a query. And Like to return the results as a plain old JSON if possible.

David Cox

Posts: 28
Registered: 7/6/07
Re: Ability to upload data a files (image) via webservices
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 6, 2016 6:46 AM   in response to: steven chesser in response to: steven chesser
I guess last thing I'd like to figure out if anyone knows, is how to easily convert a TDataSet to JSON?

Hi Steve,

I am just getting into REST myself so I am a beginner! That said, are you after the functionality of TDBXJsonTools? A bit of description is here:

http://www.andreanolanusse.com/en/tdbxjsontools-copyconvert-data-between-tdataset-tjsonobject-dbxreader/

Dave

Edited by: David Cox on Jul 6, 2016 9:46 AM
steven chesser

Posts: 401
Registered: 4/13/09
Re: Ability to upload data a files (image) via webservices
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 6, 2016 8:07 AM   in response to: David Cox in response to: David Cox
David Cox wrote:
I guess last thing I'd like to figure out if anyone knows, is how to easily convert a TDataSet to JSON?

Hi Steve,

I am just getting into REST myself so I am a beginner! That said, are you after the functionality of TDBXJsonTools? A bit of description is here:

http://www.andreanolanusse.com/en/tdbxjsontools-copyconvert-data-between-tdataset-tjsonobject-dbxreader/

Dave

Edited by: David Cox on Jul 6, 2016 9:46 AM

I saw that... it "works" but don't like the way it formats the data.

VAR qInventory : tadoquery;
..
 
aDBXReader := TDBXDataSetReader.Create (qInventory, False );
result := TDBXJSONTools.TableToJSON (aDBXReader, qInventory.RecordCount , False);
..


What it does is break it up into columns...

So if got 5 columns of data, with 100 records.

It will output 5 nodes in the json... each with 100 records in it.

Way I need it to be broken up is 100 nodes..... (each row) ... with the 5 columns of data in it.

That way the end user can look at record 1 ... and pull out any of the 5 columns easily.

Using FireDac it does this... but its a bit clunky on its break out of the data... but does work.... but more complex to
do and bit clunky on the output...

var qData  : TFDQuery;
     Connection: TFDConnection;
     fStream : tstringstream;
     FDStanStorageJSONLink1: TFDStanStorageJSONLink;
 begin
    FDStanStorageJSONLink1:= TFDStanStorageJSONLink.Create(nil);
   
 Connection := TFDConnection.Create(nil);
    Connection.Params.AddPair('DriverID','ODBC');
    Connection.Params.AddPair('DataSource','MyDB1');
    Connection.LoginPrompt := false;
 
    qData := TFDQuery.Create(nil);
    qData.Connection := Connection;
    qData.SQL.Text := mySQLstr;
    qData.Open;
    fStream := tstringstream.Create;
    qData.SaveToStream(fStream,tfdstorageformat.sfJSON);
    fStream.Position := 0;
    result    := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(fStream.DataString),0) as TJSONObject;
    freeandnil(FDStanStorageJSONLink1);
    freeandnil(qData);
    freeandnil(fStream);
    freeandnil(connection);
end;


Edited by: steven chesser on Jul 6, 2016 8:30 AM
David Cox

Posts: 28
Registered: 7/6/07
Re: Ability to upload data a files (image) via webservices
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 11, 2016 1:05 PM   in response to: steven chesser in response to: steven chesser
Steve,

Are you looking for something like the following three records I have from a test database:

[{"orderid":166,"ordernumber":"T-5678-5678","partno":"AAA","jobid":"Mix","assemblyid":null,"jobstateid":8 },
{"orderid":215,"ordernumber":"TEST MAT 1","partno":"P1000","jobid":"Western CO","assemblyid":null,"jobstateid":8 },
{"orderid":216,"ordernumber":"TEST MAT 2","partno":"P1000","jobid":"Eastern CO","assemblyid":null,"jobstateid":8 }]

If so, you may want to look at the Delphi MVC Framework by Daniel Teti:

https://www.gitbook.com/book/danieleteti/delphimvcframework/details

He also wrote "Delphi Cookbook" Second edition that gives examples of using the framework to serialize a database to JSON and back.

Dave

Edited by: David Cox on Jul 11, 2016 4:06 PM
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02