Recent

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

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 616
    • Double Dummy Solver - free download
How to copy file to web
« on: December 13, 2024, 08:10:27 pm »
i have no problem downloading files from my website using fphttpclient. I'd like some help writing a file back to my web site. This is the sample coding I saw

Code: Pascal  [Select][+][-]
  1. function PutFileContent(URL: string; Response: TStream): boolean;
  2. begin
  3.   Result := False;
  4.   with TFPHttpClient.Create(nil) do
  5.   try
  6.     Put(URL, Response);
  7.     Result := True;
  8.   finally
  9.     Free;
  10.   end;
  11. end;

but it gives an error
..exception class 'EHTTPClient' with message
Invalid protocol: "C"

For sure I would need a Username and Password, perhaps some other info, but it's not clear how to provide that.

What's an easy way to write a file to my web site? Does it depend on text or binary file?



Lazarus 3.6 FPC 3.2.2 x86_64-win64-win32/win64

Warfley

  • Hero Member
  • *****
  • Posts: 1838
Re: How to copy file to web
« Reply #1 on: December 13, 2024, 11:25:08 pm »
It depends, how is the Website upload function implemented? If the http server does not implement a way to upload files via post or put you can't use it that way

TRon

  • Hero Member
  • *****
  • Posts: 3756
Re: How to copy file to web
« Reply #2 on: December 13, 2024, 11:33:33 pm »
As warfley already mentioned.

Since it is your website  you would have to implement functionality for being able to upload files (or make use with whatever CMS is in place).

Usually hosting companies offer shell or ftp access which you can use for maintenance (including uploading files).
I do not have to remember anything anymore thanks to total-recall.

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 616
    • Double Dummy Solver - free download
Re: How to copy file to web
« Reply #3 on: December 13, 2024, 11:44:03 pm »
I can upload files using Filezilla or FireFTP without a problem. I want to upload them using a Laz app.
Lazarus 3.6 FPC 3.2.2 x86_64-win64-win32/win64

TRon

  • Hero Member
  • *****
  • Posts: 3756
Re: How to copy file to web
« Reply #4 on: December 14, 2024, 12:21:12 am »
Is your aim to let users of your website be able to upload files (including yourself) or is it only meant for you (the owner of the website) to be able to update your website (e.g. upload html pages, pictures , php scripts etc) ?

In case the latter you can use ftps or sftp protocol to upload files (the choice depends on what protocol  your host supports, see your filezilla/fireFTP settings ) . In case the former there is need to have something running on your server that is able to accept file uploads.

Depending on the answer(s) there are different paths to take.
« Last Edit: December 14, 2024, 12:23:07 am by TRon »
I do not have to remember anything anymore thanks to total-recall.

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 616
    • Double Dummy Solver - free download
Re: How to copy file to web
« Reply #5 on: December 14, 2024, 03:46:21 am »
My aim was to have an app that I and my spouse could use to upload files to the web
Lazarus 3.6 FPC 3.2.2 x86_64-win64-win32/win64

lainz

  • Hero Member
  • *****
  • Posts: 4649
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: How to copy file to web
« Reply #6 on: December 14, 2024, 05:09:38 am »
You can use ftp with your web server credentials.

Or create a php script to upload files. Ensure that it has at least a token as authentication.

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 616
    • Double Dummy Solver - free download
Re: How to copy file to web
« Reply #7 on: December 14, 2024, 07:48:09 am »
I thought the best way to do that with Laz was to use FTPSend - but that unit is no longer in Laz (if it every was) and I can't figure out how to do it inside a Windows Laz program.  I now think I want to use something in the fphttpclient unit.
Lazarus 3.6 FPC 3.2.2 x86_64-win64-win32/win64

TRon

  • Hero Member
  • *****
  • Posts: 3756
Re: How to copy file to web
« Reply #8 on: December 14, 2024, 07:56:04 am »
... Laz was to use FTPSend - but that unit is no longer in Laz (if it every was) ...
It is a unit of the synapse project. see also github.

You can install it in Lazarus using OPM.

Other than that, and perhaps you do not realize, your hints are vague at best. You have a webserver running so you ought to now what you run there and how to access. There are literally dozens of methods to 'upload' files to a webserver ranging from sending e-mails, remote shell access to running a dedicated service. Each method requires another approach. As an example, this forum software allows for attaching files that are being uploaded when you press the send message button. In order to let that work a http server is running on the webserver, along with some scripts that know what to do with those attached files when a user presses that send message link.

fphttpclient can be used in situations where a service runs on your website that actually accepts files (and know what to do with it, in case of 'sending' files). As long as you do not have anything hosted in that department the use of fphttpclient will be in vain.

Since you mentioned being able to access the server with an ftp client that means that your hosting provider at least grants ftp access, so the focus should be on that, Plenty of examples available either provided by synapse or here in the forums (and synapse is only one of several options) but.... it is important to know whether the server should be accessed using sftp or ftps as the actual implementation depends on it..

« Last Edit: December 14, 2024, 08:39:00 am by TRon »
I do not have to remember anything anymore thanks to total-recall.

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 616
    • Double Dummy Solver - free download
Re: How to copy file to web
« Reply #9 on: December 14, 2024, 09:20:16 pm »
A function that looks like something that would work is in the FTPAPI unit

Code: Pascal  [Select][+][-]
  1. Function FtpPut(Host, UserId, Passwd, Acct, Local, Remote: PChar; TransferType: Integer): Integer; cdecl;
  2.  
However, unit can't be compiled because it uses OS2Def and PMWin which are no where to be found. This FTPAPI unit is more than 20 years old and has probably been superceded. Perhaps there's a component that will achieve the same end.

This thread from last summer seems to ask a very similar question
https://forum.lazarus.freepascal.org/index.php?topic=25066.0
Lazarus 3.6 FPC 3.2.2 x86_64-win64-win32/win64

dseligo

  • Hero Member
  • *****
  • Posts: 1443
Re: How to copy file to web
« Reply #10 on: December 15, 2024, 12:36:16 am »
Why don't you use Synapse as Tron told you?

Then you can use FTP like this:
Code: Pascal  [Select][+][-]
  1. uses ... ftpsend ...
  2. ...
  3. with TFTPSend.Create do
  4. try
  5.   Username := 'username';
  6.   Password := 'password';
  7.   TargetHost := 'host';
  8.   TargetPort := 'port';
  9.   if not Login then Exit;
  10.   If not ChangeWorkingDir('remote directory') then
  11.     Exit;
  12.   DirectFileName := 'source file';
  13.   DirectFile:=True;
  14.   Result := StoreFile('destination file', False);
  15.   Logout;
  16. finally
  17.   Free;
  18. end;

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 616
    • Double Dummy Solver - free download
Re: How to copy file to web
« Reply #11 on: December 15, 2024, 12:46:49 am »
That's exactly solution I'm trying to use. However, I can not find ftpsend anywhere.  I've loaded synapse from the OPM but there is no ftpsend.pas file anywhere on my PC.  Do I need to download that file from some other source?
Lazarus 3.6 FPC 3.2.2 x86_64-win64-win32/win64

TRon

  • Hero Member
  • *****
  • Posts: 3756
Re: How to copy file to web
« Reply #12 on: December 15, 2024, 12:55:52 am »
Did you add synapse as dependency in the project manager of your project ?
I do not have to remember anything anymore thanks to total-recall.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1447
    • Lebeau Software
Re: How to copy file to web
« Reply #13 on: December 15, 2024, 01:18:08 am »
Indy is also in OPM, and it has a TIdFTP component.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

dseligo

  • Hero Member
  • *****
  • Posts: 1443
Re: How to copy file to web
« Reply #14 on: December 15, 2024, 01:48:51 am »
That's exactly solution I'm trying to use. However, I can not find ftpsend anywhere.  I've loaded synapse from the OPM but there is no ftpsend.pas file anywhere on my PC.  Do I need to download that file from some other source?

You probably just need to add laz_synapse to Required Packages in Project Inspector, like Tron said.

But it is strange what you said that you can't find ftpsend anywhere.
Open OPM, then click on "Local repo" button, and then you should see synapse40.1 directory. ftpsend.pas should be in it.

 

TinyPortal © 2005-2018