Recent

Author Topic: Downloading file from web with synapse library  (Read 6160 times)

Ronan

  • Full Member
  • ***
  • Posts: 132
Downloading file from web with synapse library
« on: October 09, 2015, 02:03:45 pm »
dear All,

I'm having dificulties with trying to download the files from the web-site using synapse functionalities. I followed each example given in that forum and under the http://wiki.freepascal.org/Synapse#From_an_FTP_server link, although all seems very easy  to study and run none of them worked for me.  I also tried the following code examples, the purpose of program is simply to allow the users to download the updates of standalone application.

For example the folllowing code doesn't login to ftp server (credentials are correct, tested with FileZilla) so didn't download the README.txt file
Code: Pascal  [Select]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. var
  3.  ftp :TFTPSend;
  4. begin
  5.      ftp := TFTPSend.Create;
  6.      ftp.TargetHost := 'ftp://mywebsite.net';
  7.      ftp.BinaryMode := true;
  8.      ftp.UserName := 'mywebsite.net';
  9.      ftp.Password := '********';
  10.      ftp.DirectFileName :='ftp://mywebsite.net/httpdocs/README.txt';
  11.      ftp.DirectFile := true;
  12.  
  13.    if not ftp.Login then
  14.         raise Exception.Create('could not login');
  15. //      progressbar1.Max:=ftp.FileSize(eFile.text);
  16.       if not ftp.RetrieveFile( 'README.txt',false) then
  17.         raise Exception.Create('file not retrieved');
  18.       ftp.Logout
  19. end;                    


I also tried via http way, but that didn't worked for me
Code: Pascal  [Select]
  1. procedure TForm1.Button3Click(Sender: TObject);
  2. var
  3.   HTTPGetResult: Boolean;
  4.   HTTPSender: THTTPSend;
  5. begin
  6.   HTTPSender := THTTPSend.Create;
  7.   try
  8.     HTTPGetResult := HTTPSender.HTTPMethod('GET', 'http://mywebsite.net/httpdocs/README.txt');
  9.     if (HTTPSender.ResultCode >= 100) and (HTTPSender.ResultCode<=299) then begin
  10.       HTTPSender.Document.SaveToFile('READMe.txt');
  11.     end;
  12.   finally
  13.     HTTPSender.Free;
  14.   end;
  15. end;
  16.  

What am I doing wrong here ?

REgards,

rvk

  • Hero Member
  • *****
  • Posts: 3663
Re: Downloading file from web with synapse library
« Reply #1 on: October 09, 2015, 02:27:01 pm »
What am I doing wrong here ?
A few things.

ftp.TargetHost should not contain the protocol. So:
Code: Pascal  [Select]
  1. ftp.TargetHost := 'mywebsite.net';

DirectFileName is where you want to store the file locally. So:
Code: Pascal  [Select]
  1. ftp.DirectFileName :='c:\temp\README.txt';

Look at the example in ftpsend.pas itself (which you could also use apart from the binary setting):
Code: Pascal  [Select]
  1. function FtpGetFile(const IP, Port, FileName, LocalFile, User, Pass: string): Boolean;
  2. begin
  3.   Result := False;
  4.   with TFTPSend.Create do
  5.   try
  6.     if User <> '' then
  7.     begin
  8.       Username := User;
  9.       Password := Pass;
  10.     end;
  11.     TargetHost := IP;
  12.     TargetPort := Port;
  13.     if not Login then
  14.       Exit;
  15.     DirectFileName := LocalFile;
  16.     DirectFile:=True;
  17.     Result := RetrieveFile(FileName, False);
  18.     Logout;
  19.   finally
  20.     Free;
  21.   end;
  22. end;
« Last Edit: October 09, 2015, 02:29:03 pm by rvk »

Ronan

  • Full Member
  • ***
  • Posts: 132
Re: Downloading file from web with synapse library
« Reply #2 on: October 09, 2015, 06:18:59 pm »
Thank you in advance,

Since there wasn't any explicit example I didn't know that I don't have to supply the protocol, now I can login and step further to the RetrieveFile part, It seems that I really don't know how to adress required file, this file is nothing but a standart isntallation Joomla license file which exist there and I simply try to retrieve it for testin purposes.

I 've tried following options w/o succes
   Filename := 'http://mywebsite.net/httpdocs/LICENSE.txt'
   Filename := 'mywebsite.net/httpdocs/LICENSE.txt'

Code: Pascal  [Select]
  1.    Result := RetrieveFile(FileName, False);  // Here result is false
  2.     Logout;
  3.   finally
  4.     Free;
  5.   end;
  6. end;

Regards,

GetMem

  • Hero Member
  • *****
  • Posts: 3453
Re: Downloading file from web with synapse library
« Reply #3 on: October 09, 2015, 06:26:39 pm »
It looks like license.txt is inside httpdocs folder, so you should change directory first.

Ronan

  • Full Member
  • ***
  • Posts: 132
Re: Downloading file from web with synapse library
« Reply #4 on: October 09, 2015, 06:36:54 pm »
Giving explicit file isn't enough for that  'http://mywebsite.net/httpdocs/LICENSE.txt' ?

If it is still not then do I have to use ftp.ChangeWorkingDir()  ?

Regards,

GetMem

  • Hero Member
  • *****
  • Posts: 3453
Re: Downloading file from web with synapse library
« Reply #5 on: October 09, 2015, 07:00:50 pm »
Try something like this:
Code: Pascal  [Select]
  1. //...
  2.   if ChangeWorkingDir('/httpdocs/') then
  3.   begin
  4.      Result := RetrieveFile('LICENSCE.txt', False);
  5.   end;
  6. //....
  7.  

rvk

  • Hero Member
  • *****
  • Posts: 3663
Re: Downloading file from web with synapse library
« Reply #6 on: October 10, 2015, 12:35:00 am »
Giving explicit file isn't enough for that  'http://mywebsite.net/httpdocs/LICENSE.txt' ?

If it is still not then do I have to use ftp.ChangeWorkingDir()  ?

Regards,
You don't need to use ChangeWorkingDir, perse. You can use the full-path to the file (relative to you ftp settings)
So you can do this:
Code: Pascal  [Select]
  1.   Result := RetrieveFile('/httpdocs/LICENSCE.txt', False);
But you need to be sure LICENSE.txt is in httpdocs. (you CAN'T use http://... etc. You need to use the path from the root of you ftp-server.

If you login via FileZilla... is you file directly in the / directory or is it in /httpdocs ? You need to use the directory structure you see in FileZilla.

GetMem

  • Hero Member
  • *****
  • Posts: 3453
Re: Downloading file from web with synapse library
« Reply #7 on: October 10, 2015, 08:15:17 am »
@rvk
You're right, it works without changing the directory.

Ronan

  • Full Member
  • ***
  • Posts: 132
Re: Downloading file from web with synapse library
« Reply #8 on: October 10, 2015, 08:48:35 am »
Ok thanks a lot, I always included the protocol that was my mistake

With my hosting service its not possible to give anonym ftp connection due to security reasons, so I tried another alternative of public file sharing system such as dropbox.com and confgured the synapse to download that file from http protocol

I tried following code below, but the downloaded file is corrupted and doesn't open at all, I also noticed that file size is not correct, original file is around 1MB but downloaded version is 176bytes there is incosistency. What is the way to properly set local file name to be the original file name, otherwise programming is assigning that local file name?

   if httpClient.HTTPMethod('GET','www.dropbox.com/s/edyukal6hw47nwd/New%20Bitmap%20Image.bmp?dl=1') then
    httpClient.Document.SaveToFile('New_Bitmap_Image.bmp');

rvk

  • Hero Member
  • *****
  • Posts: 3663
Re: Downloading file from web with synapse library
« Reply #9 on: October 10, 2015, 10:14:56 am »
With my hosting service its not possible to give anonym ftp connection due to security reasons, so I tried another alternative of public file sharing system such as dropbox.com and confgured the synapse to download that file from http protocol
Ok, so we can't use ftp because there is no anonymous access. Alright... Then for you're attempt of using dropbox.

You got a New_Bitmap_Image.bmp of 176. If you want to know what's happening you should rename that file to New_Bitmap_Image.txt and open it with an editor. You see something like this:
Code: Text  [Select]
  1. <html>
  2. <head><title>301 Moved Permanently</title></head>
  3. <body bgcolor="white">
  4. <center><h1>301 Moved Permanently</h1></center>
  5. <hr><center>nginx</center>
  6. </body>
  7. </html>
You've chosen the worst possible http-service for your purpose. Dropbox has multiple redirection schemes in place so you need to follow where you're send. That URL you gave is not a direct download link.

But do you really need dropbox in this instance?

You did say you had a web-hosting provider. They might not give you ananymous ftp access but they do provide just HTTP-access. So why don't you put that New_Bitmap_Image.bmp on there and use httpClient.HTTPMethod to get it from your own site?

It is possible to download it from dropbox but you would need to read the headers to know where you are redirected to. If putting the file on your own website (at your provider) is possible, it's much easier and your given code should work fine.

Ronan

  • Full Member
  • ***
  • Posts: 132
Re: Downloading file from web with synapse library
« Reply #10 on: October 10, 2015, 12:08:44 pm »
Thank you very much,