[RESOLVE] [ASK] Synapse and Upload File with POST to PHP scripts lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Mujie · 2015-08-30 06:56 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] $name) { if((!empty($_FILES["docfile"]['name'][$f]))) { $filename = basename($_FILES['docfile']['name'][$f]); $ext = substr($filename, strrpos($filename, '.') + 1); $newname = $upload_dir.$filename; if (!file_exists($newname)) { if ((move_uploaded_file($_FILES['docfile']['tmp_na me'][$f],$newname))) { //Whatever :) } } else { //Whatever :) } } } ?> Code: [Select]
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.. ---------------------------------------------------------------------- derek.john.evans · 2015-08-30 07:26 This worked for me (using your PHP code): Code: Pascal [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses fphttpclient; procedure PostFile(const AFileName: TFileName);const SMyFilePostURL = '<<<< INSERT URL TO PHP HERE >>>>';var LResponse: TStringStream;begin LResponse := TStringStream.Create(EmptyStr); try TFPCustomHTTPClient.SimpleFileFormPost(SMyFilePostURL, 'docfile[]', AFileName, LResponse); finally FreeAndNil(LResponse); end;end; ---------------------------------------------------------------------- Mujie · 2015-08-30 08:08 @derek : Thanks! it's working great now PS: It should add to http://wiki.freepascal.org/fphttpclient for POST method and I'm still curious with Synapse ---------------------------------------------------------------------- derek.john.evans · 2015-08-30 08:32 Quote from: Mujie on August 30, 2015, 10:08:55 am......I'm still curious with Synapse Maybe try with this function in Synapse's httpsend.pas: Code: Pascal [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function HttpPostFile(const URL, FieldName, FileName: string; const Data: TStream; const ResultData: TStrings): Boolean; Same idea, except you need to provide a TFileStream (Data). ---------------------------------------------------------------------- Mujie · 2015-08-30 08:46 Quote from: derek.john.evans on August 30, 2015, 10:32:55 amSame 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.