Recent

Author Topic: Download a file from the net  (Read 5361 times)

justnewbie

  • Full Member
  • ***
  • Posts: 227
Download a file from the net
« 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?
« Last Edit: April 10, 2018, 08:55:33 am by justnewbie »

GetMem

  • Hero Member
  • *****
  • Posts: 3757
Re: Download a file from the net
« Reply #1 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.
« Last Edit: April 06, 2018, 08:30:02 pm by GetMem »

justnewbie

  • Full Member
  • ***
  • Posts: 227
Re: Download a file from the net
« Reply #2 on: April 06, 2018, 08:29:36 pm »
@GetMem: I will study it over the weekend. Thank you very much!

justnewbie

  • Full Member
  • ***
  • Posts: 227
Re: Download a file from the net
« Reply #3 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!  :)
« Last Edit: April 08, 2018, 01:36:03 pm by justnewbie »

justnewbie

  • Full Member
  • ***
  • Posts: 227
Re: Download a file from the net
« Reply #4 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 ...

taazz

  • Hero Member
  • *****
  • Posts: 5365
Re: Download a file from the net
« Reply #5 on: April 08, 2018, 01:51:33 pm »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

justnewbie

  • Full Member
  • ***
  • Posts: 227
Re: Download a file from the net
« Reply #6 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).

taazz

  • Hero Member
  • *****
  • Posts: 5365
Re: Download a file from the net
« Reply #7 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.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

justnewbie

  • Full Member
  • ***
  • Posts: 227
Re: Download a file from the net
« Reply #8 on: April 08, 2018, 02:50:30 pm »
OK, thank you, I will study it!

UPDATE: tried, works like the dream! Thank you! :)
« Last Edit: April 08, 2018, 03:00:01 pm by justnewbie »

justnewbie

  • Full Member
  • ***
  • Posts: 227
Re: Download a file from the net
« Reply #9 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?

GetMem

  • Hero Member
  • *****
  • Posts: 3757
Re: Download a file from the net
« Reply #10 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?

justnewbie

  • Full Member
  • ***
  • Posts: 227
Re: Download a file from the net
« Reply #11 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.

GetMem

  • Hero Member
  • *****
  • Posts: 3757
Re: Download a file from the net
« Reply #12 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;
                   

« Last Edit: April 10, 2018, 11:52:06 am by GetMem »

justnewbie

  • Full Member
  • ***
  • Posts: 227
Re: Download a file from the net
« Reply #13 on: April 10, 2018, 08:57:46 am »
@GetMem: As soon as I can, will try it. In advance: thank you very much!

justnewbie

  • Full Member
  • ***
  • Posts: 227
Re: Download a file from the net
« Reply #14 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?
« Last Edit: April 10, 2018, 03:46:54 pm by justnewbie »

 

TinyPortal © 2005-2018