Sending file by post method (synapse) doesnt work lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- cpalx · 2018-03-01 04:02 hello i try to send a file using synapse to a web server (php),  but $_FILES always is empty PHP code Code: PHP  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   Client 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 function TForm1.MyHttpPostFile(const URL, FieldName, FileName: string;  const Data: TStream; const ResultData: TStrings): Boolean;var  HTTP: THTTPSend;  Bound, s: string;begin  Bound := IntToHex(Random(MaxInt), 8) + '_Synapse_boundary';  HTTP := THTTPSend.Create;  try    s := '--' + Bound + CRLF;    s := s + ' content-disposition: form-data; name="' + FieldName + '";';    s := s + ' filename="' + FileName +'"' + CRLF;    s := s + ' Authorization: ' + ediTOKEN.Text+ CRLF;    s := s + ' Content-Type: application/json' +  CRLF;    s := s + ' Content-Type: Application/octet-string' + CRLF + CRLF;    WriteStrToStream(HTTP.Document, s);    HTTP.Document.CopyFrom(Data, 0);    s := CRLF + '--' + Bound + '--' + CRLF;    WriteStrToStream(HTTP.Document, s);    HTTP.MimeType := 'multipart/form-data; boundary=' + Bound;    Result := HTTP.HTTPMethod('POST', URL);    if Result then      ResultData.LoadFromStream(HTTP.Document)    else      ResultData.Text:= 'Error Code:' + IntToStr(HTTP.ResultCode);  finally    HTTP.Free;  end;end; Button click event 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  procedure TForm1.btnSendFileClick(Sender: TObject);var   ListAnswer: TStringList;   FileStream: TFileStream;   Myfile: Array of string;begin   if not oDialog.Execute then      exit;    ListAnswer:= TStringList.Create;   FileStream:= TFileStream.Create( oDialog.FileName, fmOpenRead );   MyHttpPostFile( ediURL.Text,               ExtractFileNameWithoutExt( oDialog.FileName ),                 ExtractFileName( oDialog.FileName ),                 FileStream,               ListAnswer                 );   FreeAndNil( FileStream );    memServerAnswer.Text:= ListAnswer.Text; end;                                     and always display Code: HTML5  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i
array(0) {} ---------------------------------------------------------------------- Leledumbo · 2018-03-01 18:10 I'm a bit lazy to analyze, so please display the final HTTP.Document contents before you call HTTPMethod and let's see if it contains valid HTTP header/body for multipart/form-data request. ---------------------------------------------------------------------- cpalx · 2018-03-01 18:18 Is empty ---------------------------------------------------------------------- Leledumbo · 2018-03-01 18:33 Quote from: cpalx on March 01, 2018, 07:18:09 pmIs empty So how could you expect the server can get your request? Maybe you just need to: 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 HTTP.Document.Seek(0, soBeginning);before calling HTTPMethod, so the stream is read from the beginning instead of the end, which is of course, empty. ---------------------------------------------------------------------- rvk · 2018-03-01 19:28 AFAIK the HTTP.Document.Position is set to 0 prior to sending it in HTTPMethod. Second, I tried the code and HTTP.Document is not empty (you probably don't know how to read it back from the stream because that's what HTTP.Document is). The content is this: Code: [Select]--463F8555_Synapse_boundary  content-disposition: form-data; name="C:\Users\Rik\Documents\test"; filename="test.ini"  Authorization: 123  Content-Type: application/json  Content-Type: Application/octet-string [test_section] test=test --463F8555_Synapse_boundary--I have two big notes on that. 1) Why two Content-Types ??? 2) Why the spaces before every line? If I remove the spaces I get this as result: Code: [Select]array(1) {   ["C:\Users\Rik\Documents\test"]=>   array(5) {     ["name"]=>     string(8) "test.ini"     ["type"]=>     string(16) "application/json"     ["tmp_name"]=>     string(14) "/tmp/php2Mp7fo"     ["error"]=>     int(0)     ["size"]=>     int(259)   } } So, remove the spaces in front of all the lines and just use one content-type and it should work. ---------------------------------------------------------------------- cpalx · 2018-03-01 19:36 @rvk you are right, Thanks a lot