مشاهدة النسخة كاملة : ياشباب أريد عمل Download من FTP


المبرمج العربي
24-05-2005, 10:14 AM
السلام عليكم ورحمة الله وبركاته
الرجاء ممن يعرف الطريقة ان ينقذني بالكود
المطلوب هو عمل إنزال لملف من موقع معين عن طريق الفيجوال بيسك 6
Download from ftp address
ولكم جزيل الشكر

المبرمج العربي
26-05-2005, 05:47 PM
السلام عليكم ورحمة الله وبركاته
الرجاء ممن يعرف الطريقة ان ينقذني بالكود
المطلوب هو عمل إنزال لملف من موقع معين عن طريق الفيجوال بيسك 6
Download from ftp address
ولكم جزيل الشكر


كنت قد سألت عن طريقة لعمل Download من احد المساحات على النت بإستخدام الفيجوال بيسك ، وبفضل الله قد وجدت الحل ، فأحببت ان تشاركوني العمل :اولا يتم فتح مشروع جديد ، ومن ثم في قائمة التعريفات يتم وضع هذا الكود

Const FTP_TRANSFER_TYPE_UNKNOWN = &H0
Const FTP_TRANSFER_TYPE_ASCII = &H1
Const FTP_TRANSFER_TYPE_BINARY = &H2
Const INTERNET_DEFAULT_FTP_PORT = 21 ' default for FTP servers
Const INTERNET_SERVICE_FTP = 1
Const INTERNET_FLAG_PASSIVE = &H8000000 ' used for FTP connections
Const INTERNET_OPEN_TYPE_PRECONFIG = 0 ' use registry configuration
Const INTERNET_OPEN_TYPE_DIRECT = 1 ' direct to net
Const INTERNET_OPEN_TYPE_PROXY = 3 ' via named proxy
Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4 ' prevent using java/script/INS
Const MAX_PATH = 260
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUserName As String, ByVal sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, lpdwCurrentDirectory As Long) As Long
Private Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpRemoveDirectory Lib "wininet.dll" Alias "FtpRemoveDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
Private Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
Private Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" (ByVal hFtpSession As Long, ByVal lpszExisting As String, ByVal lpszNew As String) As Boolean
Private Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hConnect As Long, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Long, ByVal dwFlagsAndAttributes As Long, ByVal dwFlags As Long, ByRef dwContext As Long) As Boolean
Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hConnect As Long, ByVal lpszLocalFile As String, ByVal lpszNewRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
Private Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoA" (lpdwError As Long, ByVal lpszBuffer As String, lpdwBufferLength As Long) As Boolean
Private Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent As Long) As Long
Private Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long
Const PassiveConnection As Boolean = True

وهو عبارة عن تعريفات ومتغيرات في API ... قبل وضع شفرة التحميل يجب ان يكون لدينا مساحة على الانترنت ولنفترض ان لدينا المساحة التالية
اسم المساحة : www.abc.com
اسم المستخدم : abc
كلمة المرور : 123123

الان يتم وضع الشفرة على حدث الضغط على الزر:

hOpen = InternetOpen("API-Guide sample program", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
'هنا يتم الاتصال بالـ FTP
Refresh
hConnection = InternetConnect(hOpen, "www.abc.com", INTERNET_DEFAULT_FTP_PORT, "abc", "123123", INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
' انشاء مخزن مؤقت في المجلد
sOrgPath = String(MAX_PATH, 0)
الشفرة السابقة تقوم فقط بفتح الاتصال بالموقع عن طريق FTP وتعطينا رقم هذا الاتصال الى المتغير hConnection
حتى يتم الاستفاده منه في رفع الملفات .

الان يتم وضع الكود التالي والذي يقوم بإنزال ملف بإسم Temp.txt ووضعة في القرص C:\ من الموقع المذكور سابقا :

FtpGetFile hConnection, "Temp.txt ", "c:\Temp.txt ", False, 0, FTP_TRANSFER_TYPE_UNKNOWN, 0


يتم الان اغلاق الاتصال

'اغلاق ال FTP
InternetCloseHandle hConnection
اغلاق الاتصال بالانترنت
InternetCloseHandle hOpen



******