該FTP控件(FTP ActiveX OCX Control)是用Socket API實現的FTP功能客戶端控件,可以在網頁中上傳文件,還可以在多種開發環境中使用,如VB,VC, .NET, PB,Delphi,VFP等等.
主要功能:
>>上傳、下載單個/多個文件或目錄,支持斷點續傳;
>>顯示上傳的進度、傳輸速度和剩余時間等數據;
其它特性:
>>支持2G以上大文件傳輸;
>>連接中斷時自動檢測連接並續傳;
>>支持多語言
下載地址
接口API說明
|
一、屬性: |
AllowType: 允許上傳的文件類型;各個文件類型間以";"分隔;如:"txt;doc" |
Busy: FTP是否正在上傳或下載,是返回True; |
Connected: 是否已連接成功,是返回True,否返回False; |
DenyType: 禁止上傳的文件類型;各個文件類型間以";"分隔;如:"asp;aspx"; 如果某文件類型即在AllowType中也在DenyType中,則以DenyType為准,也就是判斷為禁示的文件類型; |
ErrorInfo: 發生錯誤時的錯誤信息; |
ErrorNumber: 發生錯誤時的錯誤號碼;點擊查看錯誤代碼列表 |
FileSize: 正在傳輸的文件的大小; |
KeepAliveInterval: 每隔多少秒檢測與服務器的連接;單位:秒,默認30秒 |
LangInfo: 控件的提示文字信息,可以設置該屬性以改變控件顯示的提示信息,步驟如下: 1.從控件的FtpLibrary1.LangInfo屬性獲取控件所使用的文字信息; 2.把獲取后的文字信息翻譯為其它語言; 3.把翻譯后的文字再賦給LangInfo屬性,使控件顯示其它語言的文字信息; 注:控件在中文系統中會自動顯示為英文; |
LeftTime: 當前文件傳輸的剩余時間;單位:秒 |
LocalPath: 調用Upload方法時為待上傳文件的路徑; 調用Download方法時為下載文件的保存路徑; |
MaxSpeed: 最大傳輸速度KB/s,V4.5.0.10版后支持; 如:MaxSpeed=100;表示最大傳輸為100KB/s; |
MaxSize: 在程序中設置可以傳輸文件的最大值;(單位:M) |
MessageInfo: 服務器端返回的消息,在事件OnServerMessage中獲取; |
Overwrite: 是否覆蓋;如果不覆蓋則斷點續傳; 1:程序中沒有設置該參數,則顯示"文件替換對話框";  2.設置為True,所有文件都替換; 3.設置為False,所有文件都續傳; 您也可以通過設置ReplaceIndex的值,操作上面的窗口; |
Passive: 是否使用被動模式傳送文件,默認為True; |
Password: 登錄FTP服務器的密碼; |
Percent: 文件傳輸已完成的百分比; |
Port: FTP服務器端口,默認為21; |
QueueIndex: 當前傳送文件在隊列中的序號; |
QueueTotal: 傳送隊列中的文件總數; |
RedoTimes: 上傳或下載發生錯誤時自動重新傳輸的次數; 比如參數設置為5,如果連接中斷則最多嘗試5次重新連接,如果連續嘗試5次仍不能連接,則提示錯誤; |
RemotePath: 服務器端文件路徑; 調用Upload時,該參數為上傳后文件在服務器端的保存路徑; 調用Download時,該參數為待下載的服務器端文件路徑; |
RenameRule: 重命名文件的規則,默認為:"{oldname}1",表示在原文件名的基礎上加1;如"test.htm"重命名后為"test1.htm"; |
ReplaceIndex: 控制"文件替換設置"窗口響應的按鈕,ReplaceIndex的值為: 0:顯示"文件替換對話框";  1:覆蓋; 2:續傳; 3:跳過; 4:重命名;(重命名規則由屬性"RenameRule"指定) |
ServerName: FTP服務器名稱; |
Speed: 當前傳輸的速度,單位:bytes/s; 可用"FormatSize(FtpLibrary1.Speed) & "/s"轉換為:kb/s或mb/s |
Timeout: 超時時間間隔,默認為30秒; |
Username: 登錄FTP服務器的用戶名,如果為空則相當於匿名; |
Version: 顯示控件的版本 |
|
二、方法: |
Abort(): 取消傳輸隊列中所有文件的傳輸操作; |
AbortCurrent(): 取消傳輸隊列中當前正在傳輸的文件的傳輸操作; |
Connect(): 連接FTP服務器;成功返回True,失敗返回False; |
CreateDirectory(FolderPath,Recursive): 在FTP服務器上創建目錄;成功返回True,失敗返回False; FolderPath:待創建的目錄路徑; Recursive(可選): 是否循環創建,默認為True; |
DeleteFile(FilePath): 刪除FTP服務器上的文件;成功返回True,失敗返回False; FilePath:需要刪除的文件路徑; |
Disconnect(): 關閉連接; |
Download(): '功能:下載單個文件、多個文件、整個目錄或指定目錄中按通配符(只支持*和?)指定的文件;成功返回True,失敗返回False; '說明: 1:下載一個文件:給RemotePath參數傳要下載的文件路徑; 2:下載整個目錄:給RemotePath參數傳要下載的目錄路徑,路徑最后一個字符要為"\"; 3:下載多個文件/目錄:RemotePath參數表示的文件路徑間以"|"分隔, 每個目錄路徑最后一個字符要為"\";此時LocalPath可對應為多個保存路徑,也可以只寫一個保存所有文件的路徑, 如果只有一個路徑所有文件以原文件名保存到該路徑下; 4:下載指定目錄中按通配符指定的文件:給RemotePath參數傳帶通配符的路徑;如:easewe\*.txt |
ExistDirectory(DirectoryName): 是否存在指定的FTP目錄 DirectoryName:需要檢測的目錄路徑; |
ExistFile(FileName): 是否存在指定的FTP文件.(只適用於V4.5.0.5或之后的版本) FileName:需要檢測的文件路徑; |
FormatSize(FileSize): 標准化文件大小,如果大於1M返回以M為單位的文件大小字符串,大於1KB則以KB為單位,否則以Bytes為單位; FileSize:以字節為單位的文件大小; |
FormatTime(Seconds): 格式化時間,把剩余時間格式為hh:mm:ss的形式;. Seconds:以秒為單位的時間值; 比如FormatTime(290)將返回"04:50" |
GetCurrentDirectory(): 獲取FTP服務器當前的工作目錄; |
GetFileExtension(FilePath): 獲取某路徑中文件的擴展名 FilePath:文件路徑,比如: GetFileExtension("d:\test\file.txt")將返回txt; |
GetFileInfo(FilePath): 獲取FTP服務器上的文件信息,返回格式為:名稱*大小*時間(如:測試文件.txt*1825*2011-09-01 15:58:16) FilePath:文件路徑; |
GetFileList(FolderPath): 獲取FTP服務器目錄的文件信息,返回格式為:0/1(目錄為1,文件為0)*名稱*大小*時間|.........各文件間以|分隔 ,(如:0*測試文件.txt*1825*2011-09-01 15:58:16|1*文件夾名稱*0*2011-09-01 16:18:28) FolderPath:目錄路徑; |
GetFileName(FilePath): 獲取某路徑的文件名 FilePath:文件路徑,比如: GetFileExtension("d:\test\file.txt")將返回file.txt; |
GetFileSize(FilePath): 獲取FTP服務器上文件的大小;返回-1表示沒有該文件,返回-2表示發生錯誤; FileName:文件路徑; |
GetLocalFileCount(FolderPath): 獲取客戶端指定路徑下所有文件的文件總數; FolderPath:目錄路徑; |
GetLocalFileDate(FilePath): 獲取本地文件的日期; FilePath:文件路徑; |
GetLocalFileSize(FilePath): 獲取本地文件的大小; FilePath:文件路徑; |
GetLocalFolderSize(FolderPath): 獲取客戶端指定路徑下所有文件的文件大小; FolderPath:目錄路徑; |
GetParentPath(FolderPath): 獲取指定目錄的父目錄; FolderPath:目錄路徑 |
LocalFileExists(FilePath): 判斷本地文件是否存在,存在返回True; FilePath:本地文件路徑; |
LocalFolderExists(FolderPath): 判斷本地文件夾是否存在,存在返回True; FolderPath:本地目錄路徑; |
RemoveDirectory(FolderPath): 刪除FTP服務器上的目錄;成功返回True,失敗返回False; FolderPath:目錄路徑; |
RenameFile(OldFileName,NewFileName): 重命名FTP服務器上的文件(夾);成功返回True,失敗返回False; OldFileName:原文件名; NewFileName:新文件名; |
SetCurrentDirectory(FolderPath): 設置FTP服務器上的當前目錄;成功返回True,失敗返回False; FolderPath:目錄路徑; |
ShowFolderBrowserDialog(): 顯示選擇文件夾對話框 |
ShowOpenFileDialog(): 顯示選擇文件對話框; V4.6.x.x版之后不支持參數,改用以下屬性來設置: 1.SFDFileName:文件對話框中默認顯示的文件名; 2.SFDFilter/AllowType:可選的文件類型; 3.SFDInitialDir:初始路徑; 4.SFDMultiple:是否支持多選,默認為true; 5.SFDTitle:對話框的標題; |
ShowSaveFileDialog(FileName): 顯示保存文件對話框; FileName:保存時默認顯示的文件名;
相關屬性: 1.SFDInitialDir:初始路徑; 2.SFDTitle:對話框的標題; |
Upload(): '功能:上傳單個文件、多個文件、整個目錄或指定目錄中按通配符(只支持*和?)指定的文件;成功返回True,失敗返回False; '說明: 1:上傳一個文件:直接給LocalPath參數傳要上傳的文件路徑; 2:上傳整個目錄:直接給LocalPath參數傳要上傳的目錄路徑,路徑最后一個字符要為"\"; 3:上傳多個文件/目錄:LocalPath參數表示的文件路徑間以"|"分隔, 每個目錄路徑最后一個字符要為"\";此時RemotePath可對應為多個保存路徑,也可以只寫一個保存所有文件的路徑,如果只有一個路徑所有文件以原文件名保存到該路徑下; 4:上傳指定目錄中按通配符指定的文件:給LocalPath參數傳帶通配符的路徑;如:c:\easewe\*.txt; |
|
三、事件: |
OnServerMessage: 服務器端返回消息時觸發該事件; Private Sub FtpLibrary1_OnServerMessage() Debug.Print FtpLibrary1.MessageInfo End Sub |
OnTransfer: 傳輸過程中觸發該事件,該事件用於顯示進度,傳輸速度,和剩余時間等數據: Private Sub FtpLibrary1_OnTransfer() Debug.Print FtpLibrary1.Percent & "%(" & FtpLibrary1.TransferredSize& "/" & FtpLibrary1.FileSize& ")" Debug.Print FtpLibrary1.FormatSize(FtpLibrary1.Speed) & "/s" Debug.Print FtpLibrary1.FormatTime(FtpLibrary1.LeftTime) End Sub |
|
四.示例: |
連接: FtpLibrary1.ServerName ="host" FtpLibrary1.Username ="easewe" FtpLibrary1.Password ="easewe" FtpLibrary1.Port =21 If FtpLibrary1.Connect Then '連接成功 Else '連接失敗(此時可以用FtpLibrary1.ErrorInfo 和 FtpLibrary1.ErrorNumber 獲取錯誤的描述和錯誤號) End If |
上傳單個文件: FtpLibrary1.LocalPath ="c:\easewe.txt" FtpLibrary1.RemotePath ="easewe.txt" If FtpLibrary1.Upload Then '成功 Else '失敗(此時可以用FtpLibrary1.ErrorInfo 和 FtpLibrary1.ErrorNumber 獲取錯誤的描述和錯誤號) End If |
上傳單個目錄'路徑的最后一個字符必須為'\' FtpLibrary1.LocalPath ="c:\easewe\" FtpLibrary1.RemotePath ="easewe\" If FtpLibrary1.Upload Then '成功 Else '失敗 End If |
上傳多個文件或目錄:'每個路徑使用'|'分隔 FtpLibrary1.LocalPath ="c:\easewe.txt|c:\easewe1.txt|c:\easewe\" FtpLibrary1.RemotePath ="easewe\" If FtpLibrary1.Upload Then '成功 Else '失敗 End If |
上傳通配符指定的文件: FtpLibrary1.LocalPath ="c:\*.txt" FtpLibrary1.RemotePath ="easewe\" If FtpLibrary1.Upload Then '成功 Else '失敗 End If |
下載單個文件: FtpLibrary1.RemotePath ="easewe.txt" FtpLibrary1.LocalPath ="c:\easewe.txt" If FtpLibrary1.Download Then '成功 Else '失敗(此時可以用FtpLibrary1.ErrorInfo 和 FtpLibrary1.ErrorNumber 獲取錯誤的描述和錯誤號) End If |
下載單個目錄:'路徑的最后一個字符必須為'\' FtpLibrary1.RemotePath ="easewe\" FtpLibrary1.LocalPath ="c:\easewe\" If FtpLibrary1.Download Then '成功 Else '失敗 End If |
下載多個文件或目錄:'每個路徑使用'|'分隔 FtpLibrary1.RemotePath ="easewe.txt|easewe1.txt|easewe\" FtpLibrary1.LocalPath ="c:\easewe.txt" If FtpLibrary1.Download Then '成功 Else '失敗 End If |
下載通配符指定的文件 FtpLibrary1.RemotePath ="*.txt" FtpLibrary1.LocalPath ="c:\easewe\" If FtpLibrary1.Download Then '成功 Else '失敗 End If |
|
|
Javascript 示例:返回頂部 |
|
連接FTP服務器: FtpLibrary.ServerName = "server name or ip";
FtpLibrary.Username = "Username";
FtpLibrary.Password = "Password";
FtpLibrary.Port = "21";
if(FtpLibrary.Connect()){
//Connected
}else{
alert("Failed to connect to server:\n" + FtpLibrary.ErrorInfo);
}
|
上傳文件: FtpLibrary.LocalPath = "c:\file.txt";
FtpLibrary.RemotePath = "file.txt";
if ( FtpLibrary.Upload() ){
alert("Finish!");
}else{
if(FtpLibrary.ErrorNumber!=16010){
alert("Failure!\n" + FtpLibrary.ErrorInfo);
}else{
alert(FtpLibrary.ErrorInfo);
}
}
|
上傳目錄 '最后一個字符必須為'\' FtpLibrary.LocalPath = "c:\folder\";
FtpLibrary.RemotePath = "folder\";
if ( FtpLibrary.Upload() ){
alert("Finish!");
}else{
if(FtpLibrary.ErrorNumber!=16010){
alert("Failure!\n" + FtpLibrary.ErrorInfo);
}else{
alert(FtpLibrary.ErrorInfo);
}
}
|
上傳多個文件或目錄: '多個文件以'|'分隔; FtpLibrary.LocalPath = "c:\file.txt|c:\folder\";
FtpLibrary.RemotePath = "folder\";
if ( FtpLibrary.Upload() ){
alert("Finish!");
}else{
if(FtpLibrary.ErrorNumber!=16010){
alert("Failure!\n" + FtpLibrary.ErrorInfo);
}else{
alert(FtpLibrary.ErrorInfo);
}
}
|
下載文件: 返回頂部 FtpLibrary.LocalPath = "c:\file.txt";
FtpLibrary.RemotePath = "file.txt";
if ( FtpLibrary.Download() ){
alert("Finish!");
}else{
if(FtpLibrary.ErrorNumber!=16010){
alert("Failure!\n" + FtpLibrary.ErrorInfo);
}else{
alert(FtpLibrary.ErrorInfo);
}
}
|
下載目錄: '最后一個字符必須為'\' FtpLibrary.LocalPath = "c:\folder\";
FtpLibrary.RemotePath = "folder\";
if ( FtpLibrary.Download() ){
alert("Finish!");
}else{
if(FtpLibrary.ErrorNumber!=16010){
alert("Failure!\n" + FtpLibrary.ErrorInfo);
}else{
alert(FtpLibrary.ErrorInfo);
}
}
|
下載多個文件或目錄: FtpLibrary.LocalPath = "c:\folder\";
FtpLibrary.RemotePath = "file.txt|folder\";
if ( FtpLibrary.Download() ){
alert("Finish!");
}else{
if(FtpLibrary.ErrorNumber!=16010){
alert("Failure!\n" + FtpLibrary.ErrorInfo);
}else{
alert(FtpLibrary.ErrorInfo);
}
}
|
顯示傳輸進度: //--copy to event OnTransfer of FtpLibrary
var percent = FtpLibrary.Percent;
var speed = FtpLibrary.FormatSize(FtpLibrary.Speed)+"/s";
var remainingtime = FtpLibrary.FormatTime(FtpLibrary.LeftTime);
|
獲取文件大小和時間: var fileinfo=FtpLibrary.GetFileInfo("file.txt");
if(fileinfo!=""){
var aryItem = new Array();
aryItem = fileinfo.split("*");
alert(aryItem[0] + "<" + FtpLibrary.FormatSize(aryItem[1]) + "," + aryItem[2] + ">");
}else{
if(FtpLibrary.ErrorNumber==0){
alert("File Not found!");
}else{
alert(FtpLibrary.ErrorInfo);
}
}
|
獲取目錄列表:返回頂部 var filelist = FtpLibrary.GetFileList("folder name");
var aryFiles = new Array(); var aryItem = new Array();
aryFiles = filelist.split("|");
if (aryFiles.length > 0) {
var count = aryFiles.length; var fileCount = 0, folderCount = 0;
var fileInfo = "";
for (var i = 0; i < count; i++) {
if (aryFiles[i].substr(0, 1) == "1") {
folderCount++;
aryItem = aryFiles[i].split("*");
fileInfo += aryItem[1];
fileInfo += "\n";
}
}
for (var i = 0; i < count; i++) {
if (aryFiles[i].substr(0, 1) == "0") {
fileCount++;
aryItem = aryFiles[i].split("*");
fileInfo += aryItem[1];
fileInfo += " <" + FtpLibrary.FormatSize(aryItem[2]);
fileInfo += " , " + aryItem[3] + ">";
fileInfo += "\n";
}
}
//--
if (folderCount > 0 && fileCount > 0) {
fileInfo = folderCount + " folder(s), " + fileCount + " file(s):\n----------------------------\n" + fileInfo;
} else {
if (folderCount > 0) fileInfo = folderCount + " folder(s):\n----------------------------\n" + fileInfo;
if (fileCount > 0) fileInfo = fileCount + " file(s):\n----------------------------\n" + fileInfo;
}
//--
alert(fileInfo);
} else {
if (FtpLibrary.ErrorNumber == 0) {
alert("Empty directory!");
} else {
alert(FtpLibrary.ErrorInfo);
}
} |