How to upload a file to the server using Synapse

1.1k views Asked by At

I'm trying to upload the file using the library THTTPSendEx.

My code:

procedure TForm1.Button1Click(Sender: TObject);
var
 HTTP:THTTPSendEx;
 Data:TMultipartFormDataStream;
 sHTML:string; //Recived HTML code from web
begin
if OpenDialog1.Execute then
begin
 HTTP:=THTTPSEndEx.Create;
 Data:=TMultipartFormDataStream.Create;
 try
  Data.AddFile('myFile', OpenDialog1.FileName);
  Data.DataEnd;
  if HTTP.Post('http://kaon.rghost.ru/files',Data,sHTML) then
  begin
  //Connection established
  //Check HTTP response
  if HTTP.IsSuccessfull then  //HTTP returns "200 OK" code.
  begin
    ShowMessage('File successfully posted to the server.');
  end;
  end else
  begin
   ShowMessage('Can not establish a connection to the server...'+#13+'Network is not avaliable or server socket does not exist.');
  end;
 finally
   FreeAndNil(HTTP);
   FreeAndNil(Data);
 end;
end;
end;

But nothing prints. Prompt in what a problem?

I'm sorry for the bad translation, I used Google Translater

UPDATE:

Working example for php:

$ url = 'http://kaon.rghost.ru/files'; 
$ FILENAME = 'add.png'; 

$ files = array ('file' => '@'. $ FILENAME); 
  
$ useragent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: 1.9.0.3) Gecko/2008092417 Firefox/3.0.3'; 

   $ ch = curl_init ($ url); 
   curl_setopt ($ ch, CURLOPT_HTTPHEADER, array ("Content-type: multipart / form-data")); 
   curl_setopt ($ ch, CURLOPT_HEADER, 1); 
   curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); 
   curl_setopt ($ ch, CURLOPT_USERAGENT, $ useragent); 
   curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ files); 
   $ response = curl_exec ($ ch); 

   echo $ response;
1

There are 1 answers

4
AlexLL On

For file posting, ANY web-site requires cookies and/or post params and/or valid input field name for file. That's minimum what need to post the file to the server.

You just copy example, paste yours url and load file to stream. Nope this didn't works that.

I don't see anything of required by rghost.ru for file posting in your code.