Recent

Author Topic: [RESOLVE] [ASK] Synapse and Upload File with POST to PHP scripts  (Read 5844 times)

Mujie

  • Jr. Member
  • **
  • Posts: 64
Hi,

I've a local server with LAMP and have a plan to upload any file(s) with Synapse + Lazarus to the LAMP server.

For a few days I still confuse with Synapse library because lack of sample(s) and lack of my brain of course  :) %).

This is my simple PHP scripts:
Code: [Select]
<?php
//process.php
     
$upload_dir "doc/";
      foreach (
$_FILES["docfile"]['name'] as $f => $name) {
        if((!empty(
$_FILES["docfile"]['name'][$f])))
    {
          
$filename basename($_FILES['docfile']['name'][$f]);
          
$ext substr($filenamestrrpos($filename'.') + 1);
          
$newname $upload_dir.$filename;
          if (!
file_exists($newname))
  {
    if ((move_uploaded_file($_FILES['docfile']['tmp_name'][$f],$newname)))
{
              
//Whatever :)
    }
          } else {
//Whatever :)
          
}
        }
      }
?>


Code: [Select]
<?php
//form.php
?>

<form enctype="multipart/form-data" action="fileserver/process.php" method="post">
<input required name="docfile[]" type="file" multiple="multiple">
<input type="submit" value="Send now">
</form>

Finally the Lazarus/Pascal codes which I found it from Google search result:

Code: [Select]
uses
  ..., synautil, synacode;

function HttpPostFiles(const URL: string; const Files: array of string;
  ResultData: TStrings): boolean;
var
  HTTP: THTTPSend;
  Bound: string;
  i: integer;
  Filename: string;
begin
  Bound := IntToHex(Random(MaxInt), 8) + '_Synapse_boundary';
  HTTP := THTTPSend.Create;
  try
    for i := Low(Files) to High(Files) do
      if FileExists(Files[i]) then
      begin
        Filename := ExtractFileName(Files[i]);
        WriteStrToStream(HTTP.Document, CRLF + '--' + Bound + CRLF +
           'Content-Disposition: form-data; name=' + AnsiQuotedStr(Filename, '"') +
          ';' + CRLF + #9'filename=' + AnsiQuotedStr(Filename, '"') +
          CRLF + 'Content-Type: application/octet-string' + CRLF + CRLF);
        HTTP.Document.LoadFromFile(Files[i]);
      end;
    WriteStrToStream(HTTP.Document, CRLF + '--' + Bound + '--' + CRLF);
    HTTP.MimeType := 'multipart/form-data; boundary=' + Bound;
    Result := HTTP.HTTPMethod('POST', URL);
    if Result then
      ResultData.LoadFromStream(HTTP.Document);
  finally
    HTTP.Free;
  end;
end;

procedure Tloginform.Button1Click(Sender: TObject);
var
  ResultData: TStringList;
begin
  ResultData := TStringList.Create;
  try
    if HttpPostFiles('http://192.168.43.176/fileserver/process.php',
      ['E:\development\software\lazarus\47-fileserver\test.txt', 'E:\development\software\lazarus\47-fileserver\tust.txt'], ResultData) then
      ShowMessage(ResultData.Text);
  finally
    ResultData.Free;
  end;
end; 

But, I can not produce any result with Lazarus/Pascal on my local webhost server. Please help. Many thanks.. :)             
« Last Edit: August 30, 2015, 10:09:37 am by Mujie »

derek.john.evans

  • Guest
Re: [ASK] Synapse and Upload File with POST to PHP scripts
« Reply #1 on: August 30, 2015, 09:26:45 am »
This worked for me (using your PHP code):

Code: Pascal  [Select][+][-]
  1. uses fphttpclient;
  2.  
  3. procedure PostFile(const AFileName: TFileName);
  4. const
  5.   SMyFilePostURL = '<<<< INSERT URL TO PHP HERE >>>>';
  6. var
  7.   LResponse: TStringStream;
  8. begin
  9.   LResponse := TStringStream.Create(EmptyStr);
  10.   try
  11.     TFPCustomHTTPClient.SimpleFileFormPost(SMyFilePostURL, 'docfile[]', AFileName, LResponse);
  12.   finally
  13.     FreeAndNil(LResponse);
  14.   end;
  15. end;    
  16.  
« Last Edit: October 02, 2015, 03:18:37 am by Geepster »

Mujie

  • Jr. Member
  • **
  • Posts: 64
Re: [ASK] Synapse and Upload File with POST to PHP scripts
« Reply #2 on: August 30, 2015, 10:08:55 am »
@derek : Thanks! it's working great  now :D

PS: It should add to http://wiki.freepascal.org/fphttpclient for POST method and I'm still curious with Synapse

derek.john.evans

  • Guest
Re: [ASK] Synapse and Upload File with POST to PHP scripts
« Reply #3 on: August 30, 2015, 10:32:55 am »
......I'm still curious with Synapse

Maybe try with this function in Synapse's httpsend.pas:
Code: Pascal  [Select][+][-]
  1. function HttpPostFile(const URL, FieldName, FileName: string;
  2.   const Data: TStream; const ResultData: TStrings): Boolean;  

Same idea, except you need to provide a TFileStream (Data).
« Last Edit: October 02, 2015, 03:18:56 am by Geepster »

Mujie

  • Jr. Member
  • **
  • Posts: 64
Re: [RESOLVE] [ASK] Synapse and Upload File with POST to PHP scripts
« Reply #4 on: August 30, 2015, 10:46:05 am »
Same idea, except you need to provide a TFileStream (Data).

I have experience with memory when using "stream" which memory use get bigger rapidly, so usually I use the threads and free it later.

 

TinyPortal © 2005-2018