Lazarus

Free Pascal => Beginners => Topic started by: justnewbie on April 06, 2018, 07:33:00 pm

Title: Download a file from the net
Post by: justnewbie on April 06, 2018, 07:33:00 pm
Hi Friends!
Maybe this my question will be too difficult, I really don't know.
I'd like to download a file from the net with my program. The download description shows this:
wget ...
To be honest, I have no idea how can I work with it.
Is there anyone who could show me a solution?
Title: Re: Download a file from the net
Post by: balazsszekely on April 06, 2018, 08:19:01 pm
Quote
Maybe this my question will be too difficult, I really don't know.
No. Not really.
Quote
I'd like to download a file from the net with my program. The download description shows this:
This is a native solution, you don't need any third party component, please test attached project. For http(s) downloads, you will need the openssl libraries. You should change the values in "Button1click" according to your needs. As a homework you can add a ProgressBar.
Title: Re: Download a file from the net
Post by: justnewbie on April 06, 2018, 08:29:36 pm
@GetMem: I will study it over the weekend. Thank you very much!
Title: Re: Download a file from the net
Post by: justnewbie on April 08, 2018, 01:32:14 pm
@GetMem: it is simply PERFECT! Fantastic!
Does exactly the same that I wanted.
Awesome work, thank you very much!  :)
Title: Re: Download a file from the net
Post by: justnewbie on April 08, 2018, 01:35:40 pm
Just for the extreme luxury:  O:-)
Is it possible to unzip the zip file programmatically?
If yes, its code would be the dream itself ...
Title: Re: Download a file from the net
Post by: taazz on April 08, 2018, 01:51:33 pm
http://wiki.freepascal.org/paszlib
Title: Re: Download a file from the net
Post by: justnewbie on April 08, 2018, 02:39:19 pm
I looked at http://wiki.freepascal.org/unzip and tried to download the package from http://www.info-zip.org/, but couldn't. Something certificate error (insecure connection).
Title: Re: Download a file from the net
Post by: taazz on April 08, 2018, 02:47:20 pm
paszlib is already installed with your fpc installation, that includes the zipper unit and the included TZipper class, please read the link I provided above.
Title: Re: Download a file from the net
Post by: justnewbie on April 08, 2018, 02:50:30 pm
OK, thank you, I will study it!

UPDATE: tried, works like the dream! Thank you! :)
Title: Re: Download a file from the net
Post by: justnewbie on April 08, 2018, 03:02:46 pm
Back to the original question >> download is OK (thanks  to GetMem!).
But, what about upload? Can I get an example?
Title: Re: Download a file from the net
Post by: balazsszekely on April 09, 2018, 02:32:01 pm
Quote
Back to the original question >> download is OK (thanks  to GetMem!).
But, what about upload? Can I get an example?
Do you have your own server? Where do you wish to update the files?
Title: Re: Download a file from the net
Post by: justnewbie on April 09, 2018, 06:58:38 pm
Quote
Back to the original question >> download is OK (thanks  to GetMem!).
But, what about upload? Can I get an example?
Do you have your own server? Where do you wish to update the files?
Yes, I have. I want to upload the files to my host.
Title: Re: Download a file from the net
Post by: balazsszekely on April 10, 2018, 06:45:01 am
1. PHP
Save the following code as upload.php, copy to your server, change "test" according to your needs. Make sure the server is running and "test" has write permission.
Code: PHP  [Select][+][-]
  1. <?php
  2.   if (isset($_FILES['zip']['name']) && !empty($_FILES['zip']['name']))
  3.   {
  4.     $tmp_name_zip = $_FILES['zip']['tmp_name'];
  5.  
  6.     $location = 'test/'; /*change this */
  7.     if (move_uploaded_file($tmp_name_zip, $location . $_FILES['zip']['name']))      
  8.       echo 'zipok';
  9.   }
  10. ?>
  11.  

2. Lazarus
Code: Pascal  [Select][+][-]
  1. uses fphttpclient;
  2.  
  3. function UploadFile(const AURL, AFieldName, AFileName: String; out AError: String): Boolean;
  4. var
  5.   SS: TStringStream;
  6.   HTTPClient: TFPHTTPClient;
  7. begin
  8.   Result := False;
  9.   SS := TStringStream.Create('');
  10.   try
  11.     HTTPClient := TFPHTTPClient.Create(nil);
  12.     try
  13.       try
  14.         HttpClient.FileFormPost(AURL, AFieldName, AFileName, SS);
  15.       except
  16.         on E: Exception do
  17.           AError := E.Message;
  18.       end;
  19.       Result := SS.DataString = 'zipok';
  20.     finally
  21.       HTTPClient.Free;
  22.       HTTPClient := nil;
  23.     end;
  24.   finally
  25.     SS.Free;
  26.   end;
  27. end;
  28.  
  29. procedure TForm1.Button1Click(Sender: TObject);
  30. var
  31.   ErrMsg: String;
  32. begin
  33.   if UploadFile('http://localhost/upload.php', 'zip', 'd:\test.zip', ErrMsg) then
  34.     ShowMessage('Successfully uploaded')
  35.   else
  36.     ShowMessage('Cannot upload file: ' + sLineBreak + ErrMsg);
  37. end;
                   

Title: Re: Download a file from the net
Post by: justnewbie on April 10, 2018, 08:57:46 am
@GetMem: As soon as I can, will try it. In advance: thank you very much!
Title: Re: Download a file from the net
Post by: justnewbie on April 10, 2018, 03:11:02 pm
Somehow I couldn't get it to work.
I got the 'Cannot upload file: ' message, but there is no any error message.
Previously I created a folder on the server and gave 0755 as permission.
I have no idea. (Maybe missing user/pw)?

Update: you (GetMem) wrote this above: "For http(s) downloads, you will need the openssl libraries." Interestingly for the download it worked with my https without the openssl libraries. Maybe it is not the case for uploads?
Title: Re: Download a file from the net
Post by: balazsszekely on April 10, 2018, 03:55:22 pm
"upload.php" and "test" folder should be in the same directory, namely your http server root folder. Make sure both the php file and the folder has enough privileges. If still not works check the value of SS.DataString:
Quote
//...
ShowMessage(SS.DataString) //add this line
Result := SS.DataString = 'zipok';
//...

You can download the openssl libraries from here:
32 bit: https://packages.lazarus-ide.org/openssl-1.0.2j-i386-win32.zip
64 bit: https://packages.lazarus-ide.org/openssl-1.0.2j-x64_86-win64.zip
Use the appropriate bitness and copy the dlls in the same folder with your exe.
Title: Re: Download a file from the net
Post by: justnewbie on April 10, 2018, 06:59:53 pm
Thank you, I will check this!
Title: Re: Download a file from the net
Post by: Zath on April 16, 2018, 03:55:43 pm
As a homework you can add a ProgressBar.

Sorry to jump in on your homework...

Code: Pascal  [Select][+][-]
  1. procedure TForm1.DoOnWriteStream(Sender: TObject; APosition: Int64);
  2. begin
  3.   Progressbar1.Position:=Progressbar1.Position+1;
  4.   Label1.Caption := 'Downloaded so far: ' + FormatSize(APosition);
  5.   Application.ProcessMessages;
  6. end;    
  7.  

Is that right ?
It's the only place I could get the bar to increase. However, somehow it needs the filesize and assign that as the Progresbar max.

I've been working on a very similar project so this thread is interesting.
Title: Re: Download a file from the net
Post by: balazsszekely on April 16, 2018, 05:47:08 pm
@Zath
Quote
It's the only place I could get the bar to increase. Is that right ?
Yes.

Quote
However, somehow it needs the filesize and assign that as the Progresbar max.
You can request the file size, if the server supports the feature.

PS: Alternatively use the built-in DoOnDataReceived event as in the attachment. Unfortunately there is no DoOnDataSent so for upload you still need the StreamClass from my previous post.
TinyPortal © 2005-2018