Recent

Author Topic: How to copy file to web  (Read 2293 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1529
Re: How to copy file to web
« Reply #15 on: December 16, 2024, 03:53:42 am »
Quote
You probably just need to add laz_synapse to Required Packages in Project Inspector, like Tron said.

This is right.

I use httpsend instead of ftpsend. The file name could be httpsend.pp, not httpsend.pas, etc. Not sure. But once the laz_synapse is included in the required packages, you won't try to find the position of the file.

With httpsend, I attach an example of uploading file. 

Code: Pascal  [Select][+][-]
  1. uses HttpSend;
  2. ...
  3. function Taq_r_5.UploadImage(tpid, filename: string;out Msg: string): boolean;
  4. var
  5.    AStrm: TMemoryStream;
  6.    AStrl : TStringList;
  7.    tfn: string;
  8. begin
  9.    Result:= false;
  10.  
  11.    if FileExists(filename) then begin
  12.       tfn := tpid + ExtractFileExt(filename);  // this is to process the filename at server end.
  13.       AStrm := TMemoryStream.Create;
  14.       AStrl := TStringList.Create;    // This is to receve returned Webserver content
  15.       try
  16.           AStrm.LoadFromFile(filename);
  17.           Result := HttpPostFile(URL + 'UploadImage', tpid, tfn, AStrm, AStrl);
  18.                              // (URL, FieldName, FileName, Data, ResultData)
  19.           Msg := trim(astrl.text);  // astrl.text is returned text from server
  20.           Result:= Msg = tfn;   // My webserver returns the filename if everything is done correctly.
  21.       finally
  22.         AStrl.Free;
  23.         AStrm.Free;
  24.       end;
  25.    end
  26.    else Result := False;
  27. end;
  28.  

The function to call is

      HttpPostFile(URL, FieldName, FileName, FileStream, ReturnStrings)

Here, parameters are :

- URL  : the URL that receives the file
- FieldName:  As this is "post" method, there is a fieldname. In HTML example of  <input type=file name="myfile">, it is the value of "name".
- FileName :  This is your local file name. It does not mean that this filename will be used by server to save the file, depending on the server side. Anyway it's server's role.
- File stream: the stream of file that you want to upload.
- RetrunStrings :  the returned texts from webserver will be stored here, as TStrings.

Hope this is helpful.  Msg, tfn, etc. are for my own purposes.

 

TinyPortal © 2005-2018