Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 4 of 4 total
Thread Upload Files without TFileComboBox
Thu, Jun 16 2016 5:31 AMPermanent Link

A.Kyr

Hi.
Upto now, i have only used json formatted requests (frget) to create a simple application which reads and updates database sets.
Now i am in need to upload files.
As
a) i want to know and check the size of files(s) going to be uploaded (so not permit files bigger than a specific size to be uploaded) and b) i liked the concept of using drag and drop
i tried to include the code provived in thread: http://www.elevatesoft.com/forums?action=view&category=ewb&id=ewb_demos&msg=210&page=1
So i can get the file's name and content (content in format Base64) but i dont know how to proceed next in EWB2 in order to send the name/content to server side and get back a result without the use of TFileComboBox.
I will be thankfull for any hint or code fragment as i found myself geek with html interns..

Thanks in advance
A.Kyriakos

PS:
EWB: version 2.04
Server Side: Custom made Server with KBMMW
Fri, Jun 17 2016 2:41 PMPermanent Link

R&D team @ Environment Canada

Hi

Here are a couple of snippets of code that might help. This is not a complete example (I will try to assemble one soon, and post it to the examples forum), but it should help.

This is what we did, and it works quite well. I have 2 sections here, client side, and server side. See the EWB help fies for additional help.

1.   CLIENT SIDE :

a.   use a TServerRequest, with method POST. I called mine ‘httpSaveImages’.
b.   Specify the URL to a PHP script which will decode the data and save the file
c.   Populate some standard headers for the POST request
d.   Specify the parameters and populate them (data, destination filename)
e.   Execute the POST request

Here is the snippet of code (cleaned and simplified, there might be some minor typos) :

/////////////////

//Convert your canvas (image) into Base64 ..
strMyImageData := frmMain.pntMain.canvas.convertToDataURL('image/png');

//Prepre you http request (use the 'POST' method) to send the data.
//The script will convert the Base64 into a PNG, and save it to the destination you specify
with httpSaveImages do
begin
   //clear the headers, in case something is there (from earlier use)
   requestHeaders.clear;
   requestContent.clear;

   //specify the URL of the script which will accept the data and convert it
   url := 'scripts/saveBase64AsPng.php';
   
   //add a header specify the content type
   RequestHeaders.add('Content-Type: application/x-www-form-urlencoded');
   
   //add and populate (with data) two parameters which will be used by the script..
   //.. 'imageData' .. contains the base64 data
   //.. 'imageFilename' .. contains the destination you want
   //.. the parameters are seperated by a '&'
   RequestContent.add('imageData=' + strMyImageData
                      + '&'
                      + 'imageFilename=' + strTheNameOfYourFileDestination);
   
   //finally, add a header telling it what the content length is
   RequestHeaders.add('Content-Length: ' + intToStr(length(requestContent[0])));
   
   //trigger the server request
   execute;

end;


2.   SERVER SIDE (this is done in PHP). Google 'base64 decode php' for more info.

a.   Extract the paramerts (data and destination filename) from the POST request.
b.   Do a couple strReplace. This is so that the PHP routine ‘base64_decode’ routine works properly on your base64 data from EWB client
c.   Call the ‘base64_decode’ on the data
d.   Save the file to the specified location

Here is the snippet of code (cleaned and simplified, there might be some minor typos) :

/////////////////

<?php

//THIS SCRIPT ACCEPTS A PHP 'POST' REQUEST WITH CONTENT "imageData=....")

//WE DO THIS BECAUSE THE 'imageData' can be very long and you can not reliably use 'GET' with http parameters

//THE POST REQUEST INCLUES HEADERS WHICH DEFINE THE 'Content-Type: text/plain' and 'Content-Length: xxx' where xxx is the # bytes of the said data

//get the content and the target filename
$imageFilename = $_POST['imageFilename'];

//save the POST base-64 encoded data to a PNG file..

$img = $_POST['imageData'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
file_put_contents($imageFilename . '.png', $data);


?>

////////////////////



Might seem ‘complicated’, but it really isn’t! And it works super fast/efficient.


.. Bruno




^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


A.Kyr wrote:

Hi.
Upto now, i have only used json formatted requests (frget) to create a simple application which reads and updates database sets.
Now i am in need to upload files.
As
a) i want to know and check the size of files(s) going to be uploaded (so not permit files bigger than a specific size to be uploaded) and b) i liked the concept of using drag and drop
i tried to include the code provived in thread: http://www.elevatesoft.com/forums?action=view&category=ewb&id=ewb_demos&msg=210&page=1
So i can get the file's name and content (content in format Base64) but i dont know how to proceed next in EWB2 in order to send the name/content to server side and get back a result without the use of TFileComboBox.
I will be thankfull for any hint or code fragment as i found myself geek with html interns..

Thanks in advance
A.Kyriakos

PS:
EWB: version 2.04
Server Side: Custom made Server with KBMMW
Mon, Jun 20 2016 2:47 PMPermanent Link

A.Kyr

@ Bruno Larochelle @ Environment Canada

Thanks a lot for your reply and time.
Grace of your tips i managed to send a post request that is triggering OnPostRequest in my server.
Unfortunately i didn't manage to read the submitted info probably because my server based on a KBMMW demo doesn't cover all the cases. Following your tips the post was failed to be processed for the above reason.
I tried the multipart/form-data option but then it was failed because of missing boundery and other reasons.
So, at last, i manage to send a file like this
i use a hidden THTMLForm with
TMultiLineEdit named FileData (i put there as Lines.text the filedata)
TEdit named FileName  (i put there as text the filename)
and some other TEdit for various params.
Afterwards i do a submit.
This way my server succeeds to read the data.

Anyway thanks
A.Kyriakos
Thu, Jun 23 2016 1:01 AMPermanent Link

Bruno Larochelle

.. good, you have a solution, that's the important part Smile

regards.. Bruno

A.Kyr wrote:

params.
Afterwards i do a submit.
This way my server succeeds to read the data.

Anyway thanks
A.Kyriakos

Bruno Larochelle
Logiciels Bitwise Software
Edmonton, AB, Canada
Image