RTC Forums

Subscription => Support => Topic started by: sanche on February 23, 2015, 03:03:49 PM



Title: Send File from PHP WebServer to RTC Web Server
Post by: sanche on February 23, 2015, 03:03:49 PM
Config:

1. Server 1
PHP, Default UTF-8
Send Message:
Code:
POST http://www.server2site.com/upload HTTP/1.0\r\n 
Host: server2site.com\r\n
Content-Type: multipart/form-data; boundary=1BEF0A57BE110FD467A\r\n
Content-Length: 491\r\n
\r\n data of file
------1BEF0A57BE110FD467A\r\n
Content-Disposition: form-data; name="datafile"; filename="myimage.jpg"\r\n
Content-Type: text/plain\r\n
Content-Transfer-Encoding: binary\r\n
\r\n
------1BEF0A57BE110FD467A--\r\n

2. Server 2
RTC HTTP Server

Upload Function (QuickStart\BrowserUpload)

Code:
procedure TUploaderDM.UploadProviderDataReceived(Sender: TRtcConnection);
var ADir:String;
begin
  with TRtcDataServer(Sender) do
    begin
    if Request.Method='GET' then
      begin
      Write('<html><body><form enctype="multipart/form-data" method="post">');
      Write('Type some text, if you like:<br>');
      Write('<input type="text" name="textline" size="30"><br>');
      Write('Please specify a file to upload:<br>');
      Write('<input type="file" name="datafile" size="40"><br>');
      Write('<input type="submit" value="Send">');
      Write('</form></body></html>');
      end
    else
      begin
      Request.Params.AddText(Read);
    
  if Request.Complete then
        begin
        Write('<html><body>');
        if Request.Params['datafile']<>'' then
          begin
          ADir:=GetServerDir+'Files\';
          if not DirectoryExists(ADir) then
            CreateDir(ADir);
          if Request.Params.GetFile('datafile', ADir+
             ExtractFileName(Request.Params['datafile'])) then
            Write('File "'+Request.Params.Value['datafile']+'" was uploaded to server.')
          else
            Write('Error receiving File "'+Request.Params.Value['datafile']+'".');
          end
        else
          Write('No file uploaded (DataFile parameter empty).');

        Write('</body></html>');
        end;
      end;
    end;
end;

Results:

Upload with Browser to Server2 nice work!

Upload from Server1 not work.
Params of Request included only: name, filename
IsFile name and filename = false
Sending file included by Request, but not parsed.

How to accept file from Server1?

Thank you.



Title: Re: Send File from PHP WebServer to RTC Web Server
Post by: D.Tkalcec (RTC) on February 23, 2015, 05:46:06 PM
I can not help you with questions about the use of PHP and 3rd-party Servers, but I don't see the file content body in your "POST" code. What I see is the name of the file, without content. Are you sure the code you have used in your PHP file is correct and the file you are trying to send exists on the 3rd-party Server at the location you have specified in the PHP Script?

On the RTC side, you have two options. If the content being posted to the RTC Server is compatible with the format used by Web Browsers when uploading files, then you can use the "Request.Params" property to have RTC parse the content and give you easy access to the uploaded file (as in the "BrowserUpload" example). But if the 3rd-party Client or Server is NOT using the same format as used by WebBrowsers for uploading files and sending "form post" data, then you will have to manually parse the content received. You can always use the "Read" or "ReadEx" method to get the content body sent to the RTC Server and then write your own code or use 3rd-party code for parsing that content and extracting the data you need.

Or ... instead of using some special format as an "envelope" for the file, you could try sending the file content in binary format as content body of a "PUT" request. That should work from any source and won't require special content parsing. The content you get from the "Read" or "ReadEx" method would be the actual file. You can either URL_Encode the file name as part of the URI or send it as a custom HTTP header value. To accept the file on the RTC Server side, you would use code similar to the "ClientUpload" example in the QuickStart folder.

Best Regards,
Danijel Tkalcec


Title: Re: Send File from PHP WebServer to RTC Web Server
Post by: sanche on February 23, 2015, 08:46:41 PM
PUT method works fine!

Thanks a lot!


Title: Re: Send File from PHP WebServer to RTC Web Server
Post by: D.Tkalcec (RTC) on February 23, 2015, 08:55:54 PM
Thank you for your feedback.

Best Regards,
Danijel Tkalcec