Recent

Author Topic: [SOLVED] My 1st App - FTP - Down/Upload Single File (Blank File) Problem  (Read 7475 times)

pixelink

  • Hero Member
  • *****
  • Posts: 1269
Okay... Here is my first LAZ App   :-[

FORM SETUP:
2 buttons (Download & Upload)
Using Synapse Library

I got code directly from the FTPSend.pas file towards the bottom (sample section)
Even though it's a function, I just made it to work on the click event of each button.

DOWNLOAD ISSUE:
I download "notice.css" from web server
A file shows up on my PC, but it is blank (should be 155 bytes)

UPLOAD ISSUE:
No file shows up at all on webserver

HERE IS MY CODE:
Code: Pascal  [Select][+][-]
  1. unit ftpmain;
  2. {$mode objfpc}{$H+}
  3.  
  4. interface
  5.  
  6. uses
  7.         Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, FTPsend;
  8.  
  9. type
  10. { TForm1 }
  11.  
  12. TForm1 = class(TForm)
  13.   Button1: TButton;
  14.   Button2: TButton;
  15.   procedure Button1Click(Sender: TObject);
  16.   procedure Button2Click(Sender: TObject);
  17. private
  18.   { private declarations }
  19. public
  20.   { public declarations }
  21. end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26.   //var for FTP credenials
  27.   IPHost: String = 'www.ourwz.com';
  28.   Port: String = '21';
  29.   User: String = 'XXXXXXX';
  30.  Pass: String = 'XXXXXX';
  31.  
  32. implementation
  33. {$R *.lfm}
  34. { TForm1 }
  35.  
  36. //=======================================================
  37. // DOWNLOAD BUTTON
  38. procedure TForm1.Button1Click(Sender: TObject);
  39.  
  40. var
  41.   FileToGet: String = '/public_html/technipixel/notice.css';
  42.   LocalFile: String = 'c:\Apps\notice.css';
  43.  
  44. begin
  45. with TFTPSend.Create do
  46. try
  47.         if User <> '' then
  48.    begin
  49.         Username := User;
  50.         Password := Pass;
  51.    end;
  52.         TargetHost := IPHost;
  53.         TargetPort := Port;
  54.         BinaryMode := False;
  55.         PassiveMode:= True;
  56.         if not Login then
  57.                 Exit;
  58.         DirectFileName := LocalFile;
  59.         DirectFile:=True;
  60.         RetrieveFile(FileToGet, False);
  61.         Logout;
  62. finally
  63.         Free;
  64.     ShowMessage('Done!');
  65.  end;
  66.  
  67. end;
  68.  
  69. //=======================================================
  70. // UPLOAD BUTTON
  71. procedure TForm1.Button2Click(Sender: TObject);
  72.  
  73. var
  74.   FileToGet: String = 'c:\Apps\testtextfile.txt';
  75.   PutFile: String = '/public_html/technipixel/testtextfile.txt';
  76.  
  77. begin
  78. with TFTPSend.Create do
  79. try
  80.         if User <> '' then
  81.    begin
  82.         Username := User;
  83.         Password := Pass;
  84.    end;
  85.         TargetHost := IPHost;
  86.         TargetPort := Port;
  87.         BinaryMode := False;
  88.         PassiveMode:= True;
  89.         if not Login then
  90.                 Exit;
  91.         DirectFileName := FileToGet;
  92.         DirectFile:=True;
  93.         StoreFile(PutFile, False);
  94.         Logout;
  95. finally
  96.         Free;
  97.        ShowMessage('Done!');
  98.  end;
  99. end;
  100.  
  101. //===============================================================
  102. // ORIGINAL CODE FROM FTPSEND.PAS
  103. //===============================================================
  104.  
  105. //function FtpGetFile(const IP, Port, FileName, LocalFile, User, Pass: string): Boolean;
  106. //begin
  107. //  Result := False;
  108. //  with TFTPSend.Create do
  109. //  try
  110. //    if User <> '' then
  111. //    begin
  112. //      Username := User;
  113. //      Password := Pass;
  114. //    end;
  115. //    TargetHost := IP;
  116. //    TargetPort := Port;
  117. //    if not Login then
  118. //      Exit;
  119. //    DirectFileName := LocalFile;
  120. //    DirectFile:=True;
  121. //    Result := RetrieveFile(FileName, False);
  122. //    Logout;
  123. //  finally
  124. //    Free;
  125. //  end;
  126. //end;
  127.  
  128. //function FtpPutFile(const IP, Port, FileName, LocalFile,
  129. //  User, Pass: string): Boolean;
  130. //begin
  131. //  Result := False;
  132. //  with TFTPSend.Create do
  133. //  try
  134. //    if User <> '' then
  135. //    begin
  136. //      Username := User;
  137. //      Password := Pass;
  138. //    end;
  139. //    TargetHost := IP;
  140. //    TargetPort := Port;
  141. //    if not Login then
  142. //      Exit;
  143. //    DirectFileName := LocalFile;
  144. //    DirectFile:=True;
  145. //    Result := StoreFile(FileName, False);
  146. //    Logout;
  147. //  finally
  148. //    Free;
  149. //  end;
  150. //end;
  151.  
  152. end.
  153.  
  154.  

The only thing I added to the sample code was PassiveMode and BinaryMode, trying to figure out why the files are not working properly.
Any insight into what I need to look for would be appreciated.
« Last Edit: August 20, 2016, 08:11:59 pm by technipixel »
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 4.2.0 •  VSSTUDIO(.Net) 2022 • Win11 • 32G RAM • Nvida RTX 4070 Ti Super

pixelink

  • Hero Member
  • *****
  • Posts: 1269
Re: My 1st App - FTP - Down/Upload Single File (Blank File) Problem
« Reply #1 on: August 19, 2016, 01:05:17 am »
I am looking through the Methods on this page...
http://synapse.ararat.cz/doc/help/ftpsend.TFTPSend.html

Maybe I can find something here.
I am not sure why the file I download is blank.

 ::)

Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 4.2.0 •  VSSTUDIO(.Net) 2022 • Win11 • 32G RAM • Nvida RTX 4070 Ti Super

lainz

  • Hero Member
  • *****
  • Posts: 4743
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: My 1st App - FTP - Down/Upload Single File (Blank File) Problem
« Reply #2 on: August 19, 2016, 01:54:32 am »
Works for me, I've changed the data to test for Download and is working fine.

Code: Pascal  [Select][+][-]
  1. unit ftpmain;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
  9.   StdCtrls, FTPsend;
  10.  
  11. type
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Button2: TButton;
  17.     procedure Button1Click(Sender: TObject);
  18.     procedure Button2Click(Sender: TObject);
  19.   private
  20.     { private declarations }
  21.   public
  22.     { public declarations }
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28.   //var for FTP credenials
  29.   IPHost: string = 'ftp.freepascal.org';
  30.   Port: string = '21';
  31.   User: string = 'anonymous';
  32.   Pass: string = 'anonymous@';
  33.  
  34. implementation
  35.  
  36. {$R *.lfm}
  37. { TForm1 }
  38.  
  39. //=======================================================
  40. // DOWNLOAD BUTTON
  41. procedure TForm1.Button1Click(Sender: TObject);
  42.  
  43. var
  44.   FileToGet: string = 'pub/fpc/docs-pdf/user.pdf';
  45.   LocalFile: string = 'c:\temp\user.pdf';
  46.  
  47. begin
  48.   with TFTPSend.Create do
  49.     try
  50.       if User <> '' then
  51.       begin
  52.         Username := User;
  53.         Password := Pass;
  54.       end;
  55.       TargetHost := IPHost;
  56.       TargetPort := Port;
  57.       BinaryMode := False;
  58.       PassiveMode := True;
  59.       if not Login then
  60.         Exit;
  61.       DirectFileName := LocalFile;
  62.       DirectFile := True;
  63.       RetrieveFile(FileToGet, False);
  64.       Logout;
  65.     finally
  66.       Free;
  67.       ShowMessage('Done!');
  68.     end;
  69.  
  70. end;
  71.  
  72. //=======================================================
  73. // UPLOAD BUTTON
  74. procedure TForm1.Button2Click(Sender: TObject);
  75.  
  76. var
  77.   FileToGet: string = 'c:\Apps\testtextfile.txt';
  78.   PutFile: string = '/public_html/technipixel/testtextfile.txt';
  79.  
  80. begin
  81.   with TFTPSend.Create do
  82.     try
  83.       if User <> '' then
  84.       begin
  85.         Username := User;
  86.         Password := Pass;
  87.       end;
  88.       TargetHost := IPHost;
  89.       TargetPort := Port;
  90.       BinaryMode := False;
  91.       PassiveMode := True;
  92.       if not Login then
  93.         Exit;
  94.       DirectFileName := FileToGet;
  95.       DirectFile := True;
  96.       StoreFile(PutFile, False);
  97.       Logout;
  98.     finally
  99.       Free;
  100.       ShowMessage('Done!');
  101.     end;
  102. end;
  103.  
  104. //===============================================================
  105. // ORIGINAL CODE FROM FTPSEND.PAS
  106. //===============================================================
  107.  
  108. //function FtpGetFile(const IP, Port, FileName, LocalFile, User, Pass: string): Boolean;
  109. //begin
  110. //  Result := False;
  111. //  with TFTPSend.Create do
  112. //  try
  113. //    if User <> '' then
  114. //    begin
  115. //      Username := User;
  116. //      Password := Pass;
  117. //    end;
  118. //    TargetHost := IP;
  119. //    TargetPort := Port;
  120. //    if not Login then
  121. //      Exit;
  122. //    DirectFileName := LocalFile;
  123. //    DirectFile:=True;
  124. //    Result := RetrieveFile(FileName, False);
  125. //    Logout;
  126. //  finally
  127. //    Free;
  128. //  end;
  129. //end;
  130.  
  131. //function FtpPutFile(const IP, Port, FileName, LocalFile,
  132. //  User, Pass: string): Boolean;
  133. //begin
  134. //  Result := False;
  135. //  with TFTPSend.Create do
  136. //  try
  137. //    if User <> '' then
  138. //    begin
  139. //      Username := User;
  140. //      Password := Pass;
  141. //    end;
  142. //    TargetHost := IP;
  143. //    TargetPort := Port;
  144. //    if not Login then
  145. //      Exit;
  146. //    DirectFileName := LocalFile;
  147. //    DirectFile:=True;
  148. //    Result := StoreFile(FileName, False);
  149. //    Logout;
  150. //  finally
  151. //    Free;
  152. //  end;
  153. //end;
  154.  
  155. end.

pixelink

  • Hero Member
  • *****
  • Posts: 1269
Re: My 1st App - FTP - Down/Upload Single File (Blank File) Problem
« Reply #3 on: August 19, 2016, 02:10:27 am »
The only difference I see is that you use the protocol ftp.

Let me try that...

Nope didn't work using ftp.ourwz.com

Hmmm... doesn't make sense,
I used the same credentials as my .NET utility that does the same thing.

I bet it something so simple.
  ::)
« Last Edit: August 19, 2016, 02:13:51 am by technipixel »
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 4.2.0 •  VSSTUDIO(.Net) 2022 • Win11 • 32G RAM • Nvida RTX 4070 Ti Super

pixelink

  • Hero Member
  • *****
  • Posts: 1269
Re: My 1st App - FTP - Down/Upload Single File (Blank File) Problem
« Reply #4 on: August 19, 2016, 02:35:51 am »
Well... I put PassiveMode to False and put Binary to True

At least I got the Windows FireWall "Allow To Access" window.

But, file still transferred with no data in file.
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 4.2.0 •  VSSTUDIO(.Net) 2022 • Win11 • 32G RAM • Nvida RTX 4070 Ti Super

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: My 1st App - FTP - Down/Upload Single File (Blank File) Problem
« Reply #5 on: August 19, 2016, 02:57:28 am »
The correct address is:

ftp://www.ourwz.com/public_html/technipixel/notice.css

which looks a little odd to me with the "www", since normally an FTP URL would look like the one lainz tested:

ftp://ftp.freepascal.org


pixelink

  • Hero Member
  • *****
  • Posts: 1269
Re: My 1st App - FTP - Down/Upload Single File (Blank File) Problem
« Reply #6 on: August 19, 2016, 03:04:27 am »
The correct address is:

ftp://www.ourwz.com/public_html/technipixel/notice.css

which looks a little odd to me with the "www", since normally an FTP URL would look like the one lainz tested:

ftp://ftp.freepascal.org


Nope... tried that.

This is what works in my .NET
ftp://ftp.ourwz.com/public_html/technipixel/notice.css

But, not in my LAZ utility

Went through the debugger and everything seemed okay.
No errors

Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 4.2.0 •  VSSTUDIO(.Net) 2022 • Win11 • 32G RAM • Nvida RTX 4070 Ti Super

pixelink

  • Hero Member
  • *****
  • Posts: 1269
Re: My 1st App - FTP - Down/Upload Single File (Blank File) Problem
« Reply #7 on: August 19, 2016, 03:15:01 am »
Well... finally got the download to work.

Host: 'ftp.ourwz.com'
&
path was '/public_html/technipixel/notice.css'

I had the path at the beginning, but wrong IP
Then when lainz said it worked I remove the "/" in front of "public_html

Whew!!
 8-)

Now unto the upload...

UPDATE:
Upload works!!!

Awesome... THANKS EVERYONE.
MY first FPC is almost done.

Just gonna add a check box to switch on/off which file to upload/download
« Last Edit: August 19, 2016, 03:17:32 am by technipixel »
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 4.2.0 •  VSSTUDIO(.Net) 2022 • Win11 • 32G RAM • Nvida RTX 4070 Ti Super

lainz

  • Hero Member
  • *****
  • Posts: 4743
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: My 1st App - FTP - Down/Upload Single File (Blank File) Problem
« Reply #8 on: August 19, 2016, 03:34:26 am »
Congratulations!  :)

pixelink

  • Hero Member
  • *****
  • Posts: 1269
Re: My 1st App - FTP - Down/Upload Single File (Blank File) Problem
« Reply #9 on: August 19, 2016, 04:41:01 am »
THANKS LAINz


Whats is the best-proper if statement??
I want to learn it correctly (see several ways to do it)

If Checkbox1.Check = False Then
    //do something
If Checkbox1.Check = True Then
    //do something
else
    //do something

-----  OR ------

If Checkbox1.Check = False Then
   begin
      //do something
      If Checkbox1.Check = True Then
      //do something
      else
      //do something
end;
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 4.2.0 •  VSSTUDIO(.Net) 2022 • Win11 • 32G RAM • Nvida RTX 4070 Ti Super

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: My 1st App - FTP - Down/Upload Single File (Blank File) Problem
« Reply #10 on: August 19, 2016, 06:41:39 am »
Code: Pascal  [Select][+][-]
  1. If Checkbox1.Check = False Then
  2. begin
  3.   //do something
  4.   If Checkbox1.Check = True Then
  5.   // this condition is never met
  6.   else
  7.   // this condition is alway met
  8. end;
  9.  

Why ?

Because you encapsulated the first if condition with a begin .. end statement.

This part between begin...end will only be 'executed' when "checkbox1.Check = False".

Between this begin...end block the status (true/false) of the property (Check ) will not change so testing this condition (again and expecting it to be different)  is completely pointless.

It's usually better to write something like:
Code: Pascal  [Select][+][-]
  1. If Checkbox1.Check then
  2. begin
  3.   // do something when Checkbox1.Check is true
  4. end
  5. else
  6. begin
  7.   // do something when Checkbox1.Check is false
  8. end;
  9.  

pixelink

  • Hero Member
  • *****
  • Posts: 1269
Re: My 1st App - FTP - Down/Upload Single File (Blank File) Problem
« Reply #11 on: August 19, 2016, 11:55:07 am »
Well.... THANK YOU TO EVERYONE WHO HELPED ME
Got my 1st LAZ App built.
And I can run it from a USB drive - NO .NET (AWESOME!!!)

What this Utility does is simply send a CSS file to my webserver based on the checkbox "Show Notification" selection.
Below is a screen shot of all the files used.

The noticeblock.txt and noticenone.txt files contain the CSS code.
Only difference is that the css "display" tag is different. (display:block or display:none)

What happens after I upload is if I checked the "Show Notification" checkbox... a yellow notice appears on the top of my website.
So, basically this utility just shows or hides an html DIV

As you will see in the code... very simple.

1) var is set when checkbox is selected
2) Clicking the upload button simply evaluates the var
3) We then delete the notice.css file
4) Copy and rename one of the TXT files (based on var) to notice.css
5) Then we upload the CSS file using Synapse FTPSend.pas

Only requirement is that you have to have Synapse and "uses" will be FTPSend
That's it!

HERE IS THE ENTIRE CODE:
Code: Pascal  [Select][+][-]
  1. unit main;
  2. {$mode objfpc}{$H+}
  3.  
  4. interface
  5.  
  6. uses
  7.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  8.   ExtCtrls, FTPSend;
  9.  
  10. type
  11. { TForm1 }
  12.  
  13.   TForm1 = class(TForm)
  14.     Button1: TButton;
  15.     CheckBox1: TCheckBox;
  16.     Image1: TImage;
  17.     Label1: TLabel;
  18.     procedure Button1Click(Sender: TObject);
  19.     procedure CheckBox1Change(Sender: TObject);
  20.   private
  21.     { private declarations }
  22.   public
  23.     { public declarations }
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29.   //var for FTP
  30.   IPHost: String = '**********';
  31.   Port: String = '21';
  32.   User: String = '*********';
  33.         Pass: String = '********';
  34.   PassMd: Boolean = True;
  35.   BinaryMd: Boolean = True;
  36.   DirectF: Boolean = True;
  37.  
  38.   //var for file handling
  39.   DisplayType: String = 'NONE';
  40.   defaultFile: String = 'notice.css';
  41.   newFile: String = 'noticenone.txt';
  42.   PutFile: String = '/public_html/technipixel/notice.css';
  43.  
  44. implementation
  45. {$R *.lfm}
  46. { TForm1 }
  47.  
  48. //=======================================================
  49. // UPLOAD BUTTON
  50. procedure TForm1.Button1Click(Sender: TObject);
  51.  
  52. begin
  53.   //remove CSS file
  54.   DeleteFile('notice.css');
  55.  
  56.   //set txt file based on checkbox
  57.   If DisplayType = 'BLOCK' Then
  58.     newFile := 'noticeblock.txt';
  59.   If DisplayType = 'NONE' Then
  60.     newFile := 'noticenone.txt';
  61.  
  62.   //copy file and rename to CSS
  63.   CopyFile(newFile,defaultFILE);
  64.  
  65.   //send to webserver vis FTP
  66.         with TFTPSend.Create do
  67.         try
  68.                 if User <> '' then
  69.         begin
  70.         Username := User;
  71.         Password := Pass;
  72.         end;
  73.         TargetHost := IPHost;
  74.         TargetPort := Port;
  75.     BinaryMode := BinaryMd;
  76.     PassiveMode:= PassMd;
  77.         if not Login then
  78.                 Exit;
  79.         DirectFileName := defaultFile;
  80.         DirectFile := DirectF;
  81.     StoreFile(PutFile, False);
  82.         Logout;
  83.         finally
  84.         Free;
  85.     MessageDlg('Upload Complete', 'CSS File Has Been Uploaded!', mtInformation, [mbOK], 0);
  86.  end;
  87.  
  88. end;
  89.  
  90. procedure TForm1.CheckBox1Change(Sender: TObject);
  91. begin
  92.   //check for checkbox
  93.   If Checkbox1.Checked = False Then
  94.         DisplayType:='NONE';
  95.   If Checkbox1.Checked = True Then
  96.         DisplayType:='BLOCK';
  97. end;
  98.  
  99. end.
  100.          
  101.  

WHY I MADE THIS
Often times I need to display a notice that I am out of my office (I work from home) or closed for a period of time.
Instead of opening Dreamweaver and editing the CSS every time, then open FileZilla and upload takes about 5 minutes.
This utility does it all for me in 10 seconds!

Again... THANKS
Now that my first small app is out of the way and I understand the code structure in the edior... on to a bigger project!!!
 :D
« Last Edit: August 19, 2016, 12:05:36 pm by technipixel »
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 4.2.0 •  VSSTUDIO(.Net) 2022 • Win11 • 32G RAM • Nvida RTX 4070 Ti Super

Zath

  • Sr. Member
  • ****
  • Posts: 391
Nice work.

I might (if you don't mind) try and use it for my online game server.
If BT change my IP or the server is down for maintenance I can use it to write to a forum or web site giving players a heads up.

pixelink

  • Hero Member
  • *****
  • Posts: 1269
Nice work.

I might (if you don't mind) try and use it for my online game server.
If BT change my IP or the server is down for maintenance I can use it to write to a forum or web site giving players a heads up.

Sure, go ahead
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 4.2.0 •  VSSTUDIO(.Net) 2022 • Win11 • 32G RAM • Nvida RTX 4070 Ti Super

hrayon

  • Full Member
  • ***
  • Posts: 123

 

TinyPortal © 2005-2018