PDA

View Full Version : help with ftp connect



CAI
09-29-2005, 07:13 PM
Hello,

I am wanna use most of this example script
but only one thing different

but i do'nt know how i must do this

i am using this but i want a secure connection only thru

a button connection and not thru a login and pass etc

how can i do this who can help


-- Connect to the FTP server
function Connect()
-- get the hostname/IP address
local strHostname = Input.GetText("Hostname_input");

-- make sure the hostname/IP address is valid
if FTP.IsValidHostname(strHostname) then

-- the hostname/IP address is valid, so
-- get the other parameters
local strUsername = Input.GetText("Username_input");
local strPassword = Input.GetText("Password_input");
local strPath = Input.GetText("Path_input");

--[[ if username is blank, we want to connect anonymously...
to do that we need to omit the username and password parameters
(otherwise we'll end up passing "" as the username and password) ]]
if(strUsername == "") then
FTP.Connect(strHostname);
else
FTP.Connect(strHostname, strUsername, strPassword);
end

local err = Application.GetLastError();
if err == FTP.OK then
-- FTP.Connect succeeded!

-- make sure the transfer type is set correctly (in case the user clicked
-- the buttons while disconnected)
if Button.GetState("Auto_button") == BTN_DOWN then
FTP.SetTransferType(FTP.AUTO);
elseif Button.GetState("ASCII_button") == BTN_DOWN then
FTP.SetTransferType(FTP.ASCII);
elseif Button.GetState("Binary_button") == BTN_DOWN then
FTP.SetTransferType(FTP.BINARY);
end

-- did the user enter a path?
if (strPath ~= "") then

-- try to navigate to that path
FTP.ChangeDir(strPath);

err = Application.GetLastError();
if err ~= FTP.OK then
-- FTP.ChangeDir error!
-- append the error message to the text in the paragraph object
Paragraph.SetText("Log_paragraph", Paragraph.GetText("Log_paragraph").."* Error: " .. _tblErrorMessages[err] .."\r\n");
end
end

-- call our function to update the directory list
UpdateList();

else
-- FTP.Connect error!
-- append the error message to the text in the paragraph object
Paragraph.SetText("Log_paragraph", Paragraph.GetText("Log_paragraph").."* Error: " .. _tblErrorMessages[err] .."\r\n");
end
else
-- the hostname/IP address isn't valid
-- append the error message to the text in the paragraph object
Paragraph.SetText("Log_paragraph", Paragraph.GetText("Log_paragraph").."* Error: Invalid hostname.\r\n");
end
end

-- navigate to the path in the Path_input object
function Go()
-- get the path that the user entered
local strPath = Input.GetText("Path_input");

-- if it isn't blank...
if (strPath ~= "") then

-- navigate to the path
FTP.ChangeDir(strPath);
local err = Application.GetLastError();
if err ~= FTP.OK then
-- FTP.ChangeDir error!
-- append the error message to the text in the paragraph object
Paragraph.SetText("Log_paragraph", Paragraph.GetText("Log_paragraph").."* Error: " .. _tblErrorMessages[err] .."\r\n");
end

-- update the path (moving the text cursor to the end of the text)
UpdatePath(true);

-- update the directory list
UpdateList();
end
end

-- Download the currently selected file from the FTP server
function Download()
-- get selected item
local tbSelected = ListBox.GetSelected("DirList_listbox");

if not tbSelected then
-- no file selected!
Dialog.Message("No File Selected", "Please select a file first.",MB_OK,MB_ICONEXCLAMATION);
-- exit from this function
return;
end

-- we can assume it's the first item since
-- we don't have multiple selection enabled for this listbox
local nSelected = tbSelected[1];

-- get the item data for the selected item
local strData = ListBox.GetItemData("DirList_listbox",nSelected);

--[[ note: when we populated the list box, we stored the word FILE in the item data
so we could identify files ]]

-- is the selected item a file?
if strData == "FILE" then

-- selected item is a file

-- get the item text, which in this case is the actual filename
local strSourceFile = ListBox.GetItemText("DirList_listbox",nSelected);

-- ask the user for a filename to save the file to
local tbFiles = Dialog.FileBrowse(false, "Save As", MyDocumentsFolder, "All Files (*.*)|*.*|", strSourceFile, "", false, false)

if tbFiles == nil then
-- an error occurred
err = Application.GetLastError();
Dialog.Message("Error", _tblErrorMessages[err], MB_OK, MB_ICONEXCLAMATION);

-- exit from this function
return;
end

-- did the user cancel out of the dialog?
if tbFiles[1] == "CANCEL" then
-- exit from this function
return;
end

-- they didn't cancel...we should have a file in the table
-- we can assume it's the first item since we set MultipleSelection to false
-- in the Dialog.FileBrowse action
local strDestFile = tbFiles[1];

-- does the local file already exist?
if File.DoesExist(strDestFile) then
-- destination file already exists, ask the user if it's ok to replace it
local nResult = Dialog.Message("File Already Exists!", "Overwrite the existing file?", MB_OKCANCEL, MB_ICONQUESTION, MB_DEFBUTTON1);
if nResult == IDCANCEL then
-- exit from this function
return;
end
end

-- show the status dialog and enable the cancel button
StatusDlg.ShowCancelButton();
StatusDlg.Show();

-- download the file
FTP.Download(strSourceFile, strDestFile);
local err = Application.GetLastError();

-- hide the status dialog
StatusDlg.Hide();

if err ~= FTP.OK then
-- FTP.Download error!
-- append the error message to the log
Paragraph.SetText("Log_paragraph", Paragraph.GetText("Log_paragraph").."* Error: " .. _tblErrorMessages[err] .."\r\n");
end
else

-- selected item is a folder

-- is it our special "up" folder?
if strData == ".." then
-- navigate one level up
FTP.CdUp();
else
-- navigate down into the selected folder
FTP.ChangeDir(strData);
end

-- update the path and directory list
UpdatePath();
UpdateList();
end
end

-- cbCommand: callback function for commands sent to the server
function cbCommand(strCommand)

-- make sure we don't display the login password
-- when it gets sent
if String.Left(strCommand, 4) == "PASS" then
strCommand = "PASS ****\r\n";
end

-- a prefix to make commands stand out
local strPrefix = ">>\t";

-- get the current paragraph text so we can append to it
local strLog = Paragraph.GetText("Log_paragraph");

-- append the command string and write the text back to the paragraph
Paragraph.SetText("Log_paragraph",strLog..strPrefix..strCommand);

-- scroll the paragraph to the bottom
Paragraph.SetScrollPos("Log_paragraph",9999999999);
end

-- cbResponse: callback function for commands received from the server
function cbResponse(strResponse)

-- a prefix to make responses stand out
local strPrefix = "\t";

-- get the current paragraph text so we can append to it
local strLog = Paragraph.GetText("Log_paragraph");

-- append the response string and write the text back to the paragraph
Paragraph.SetText("Log_paragraph",strLog..strPrefix..strResponse);

-- scroll the paragraph to the bottom
Paragraph.SetScrollPos("Log_paragraph",9999999999);
end

-- register our command callback function
-- so it gets called whenever an FTP command is sent
FTP.SetCommandCallback(cbCommand);

-- register our response callback function
-- so it gets called whenever the server responds
FTP.SetResponseCallback(cbResponse);

-- display the current directory contents in DirList_listbox
function UpdateList()
-- get the folders and files separately so we can display
-- the folders first (at the top of the list box)

local tbFolders = FTP.ListFolders();
local tbFiles = FTP.ListFiles();

-- empty the listbox
ListBox.DeleteItem("DirList_listbox",LB_ALLITEMS);

-- are we below the root dir?
if Input.GetText("Path_input") ~= "/" then
-- we're at least one level "down"
-- so add a special "up" folder at the top of the list box
ListBox.AddItem("DirList_listbox","[ ] ..","..");
end

-- add the folders to the list box
-- note that we store the undecorated folder name in the item data so we can
-- get the folder name from the list box without any parsing
for i = 1,tbFolders.Count do
ListBox.AddItem("DirList_listbox","[ ] "..tbFolders[i].Name,tbFolders[i].Name);
end

-- add the files to the list box
-- note that we store the word "FILE" in the item data so we can identify
-- which items are files and which are folders
for i = 1,tbFiles.Count do
ListBox.AddItem("DirList_listbox",tbFiles[i].Name,"FILE");
end
end

-- update the path in the 'Path_input' input object
-- to reflect the current directory at the server
-- if bMoveTextCursor is true, the insertion point will be moved
-- to the end of the text in the input field
function UpdatePath(bMoveTextCursor)

local strCurrentDir = FTP.GetCurrentDir();

Input.SetText("Path_input",strCurrentDir);

if bMoveTextCursor then
-- move the insertion point to the end of the text
local nPos = String.Length(strCurrentDir) + 1;
Input.SetSelection("Path_input", nPos, nPos);
end
end

-- empty the Log_paragraph object
Paragraph.SetText("Log_paragraph","");

-- empty the directory list
ListBox.DeleteItem("DirList_listbox",LB_ALLITEMS);


everything but only not thru a login and password text file

but simple a button and then connect

Please help !!!


PS sorry for the bad english :lol


Regards,


CAI