Hello ,
is there anyway to send data to FTP without using any OCX like Winsock or MSINET ?
Thanks
Printable View
Hello ,
is there anyway to send data to FTP without using any OCX like Winsock or MSINET ?
Thanks
you can do it quite easily using the ftp and internet API functions
do you a simple code ?Quote:
Originally Posted by westconn1
Thanks
in the general section
vb Code:
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 Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
in a command button or similar
vb Code:
Dim session As Long, server As Long, ftpserver As String, user As String, pass As String Dim ret As Long, localfile As String, remotefile As String, pic As String ftpserver = "" 'server user = "" ' username pass = "" ' passsword localfile = App.Path & "\test.htm" remotefile = LCase(List1.Text) & ".html" If List1.Text = "Blogs" Then blog ' put comments box code If vbNo = MsgBox("Do you wish to upload the page you previewed to the " & List1.Text & " folder on the website", vbYesNo, "Upload?") Then Unload Me: Exit Sub Me.MousePointer = vbHourglass Me.Caption = "Uploading..........." 'session = InternetOpen(App.EXEName, INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, INTERNET_FLAG_NO_CACHE_WRITE) 'server = InternetConnect(session, ftpserver, INTERNET_INVALID_PORT_NUMBER, user, pass, 1, 0, 0) session = InternetOpen(App.EXEName, INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, INTERNET_FLAG_NO_CACHE_WRITE) server = InternetConnect(session, ftpserver, INTERNET_INVALID_PORT_NUMBER, user, pass, 1, INTERNET_FLAG_PASSIVE, 0) 'session = InternetOpen(App.EXEName, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, INTERNET_FLAG_NO_CACHE_WRITE) 'server = InternetConnect(session, ftpserver, INTERNET_INVALID_PORT_NUMBER, user, pass, 1, INTERNET_FLAG_PASSIVE, 0) Dim pData As WIN32_FIND_DATA, hFind As Long, lRet As Long, mypath As String 'set the graphics mode to persistent '' Me.AutoRedraw = True 'create a buffer pData.cFileName = String(MAX_PATH, 0) 'find the first file hFind = FtpFindFirstFile(server, "*.", pData, 0, 0) mypath = String(MAX_PATH, 0) FtpGetCurrentDirectory server, mypath, Len(mypath) ret = FtpSetCurrentDirectory(server, "/www/" & LCase(List1.Text)) 'ret = FtpSetCurrentDirectory(server, List1.Text) 'mypath = Space(255) If Not ret = 0 Then ret = FtpPutFile(server, localfile, remotefile, FTP_TRANSFER_TYPE_BINARY, 0) If Not ret = 1 Then MsgBox localfile & " failed to upload" pic = Dir(App.Path & "\pic*.jpg") Do While Len(pic) > 0 ret = FtpPutFile(server, App.Path & "\" & pic, pic, FTP_TRANSFER_TYPE_BINARY, 0) If Not ret = 1 Then MsgBox App.Path & "\" & pic & " failed to upload" pic = Dir Loop End If InternetCloseHandle server InternetCloseHandle session Me.MousePointer = vbNormal MsgBox "uploading complete"
hi westconn1 ,
sorry for been too late
but i tried your code & didnt work for me
i have no error but i havent received my File in FTP
Thanks
this line
ret = FtpSetCurrentDirectory(server, "/www/" & LCase(List1.Text))
sets the destination folder, you must change that to a valid folder on your ftp server and you need to have set the username and password etc
sorry but dont work for me i set it to the root directly
i have done all tips but no it say uploading complete but i cant find anything
if you could try a simple file from your hardrive & send it to ftp to the root
& replay to me
Thanks
here you my last code
Code:Private Sub Form_Load()
Dim hConnection As Long, hOpen As Long, sOrgPath As String
'open an internet connection
hOpen = InternetOpen("API-Guide sample program", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
'connect to the FTP server
hConnection = InternetConnect(hOpen, "ftp://....", INTERNET_DEFAULT_FTP_PORT, "...", "....", INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
'create a buffer to store the original directory
sOrgPath = String(MAX_PATH, 0)
'get the directory
FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
'create a new directory 'test'
FtpCreateDirectory hConnection, "test"
'set the current directory to 'root/test'
FtpSetCurrentDirectory hConnection, "test"
'upload the file
FtpPutFile hConnection, "C:\\setup.log", "setup.log", FTP_TRANSFER_TYPE_BINARY, 0
'close the FTP connection
InternetCloseHandle hConnection
'close the internet connection
InternetCloseHandle hOpen
End Sub
if you want me to help more pm me with your ftp server, login and password, and i will test the code to make sure it works to your server
the code i posted is the exact code i use for one specific connection
i dont think that there is a problem with ftp
because i was using Msinet & working well
i just want you if its possible of course to create a simple txt file in your harddrive & send it to ftp directly to root path using only FtpPutFile
try it plz & show me your code
Thanks
here is as simple as it can get, this is from a working app, i have removed the ftp details as you can seevb Code:
Dim session As Long, server As Long, ftpserver As String, user As String, pass As String Dim ret As Long, localfile As String, remotefile As String ftpserver = "" user = "" pass = "" localfile = App.Path & "\" & fname remotefile = fname session = InternetOpen(App.EXEName, INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, INTERNET_FLAG_NO_CACHE_WRITE) server = InternetConnect(session, ftpserver, INTERNET_INVALID_PORT_NUMBER, user, pass, 1, 0, 0) ret = FtpPutFile(server, localfile, remotefile, FTP_TRANSFER_TYPE_BINARY, 0) InternetCloseHandle server InternetCloseHandle session
i never use c:\ to write files to so as you can see i used app.path in this case, the file is copied to the root folder on the ftp server by default, fname was passed to the sub from the calling sub
sorry but i dont know why but doesnt work
i spent a lot of time on it but no error with no file in ftp
so i will use ocx work fine for me
whatever works for you is good
don't know why the api is not working, i have no problem with it
i dont know m8 ,
i would be better for me to work with API rather Than an ocx
but no luck here you my code & test it if it work for you m8
Thanks
is your server on the internet or a local server?
i can only test to server that i have access to
i changed the server, user, pass and file folder, added ftp module
ran your form, then opened the file in my browser, showed content of file correctly.
if you want me to test to your server (if on internet) then pm me, server address, username and pass
hi m8 ,
what ftp module did you add to the code ?
module contains all the api declares and constants for the ftp and internet functions, as posted in above in #4, though there they are private and should go in the general section of the form as i stated with the code
i have in a module, so i can just add the module to any project
hi westconn1,
can you attach your project
i just opened you uploaded form, added the module i attach here, never saved the project
hi ,
i appreciate very much you help westconn1 & i dont know what happend to me but this Ftp dont want to work for me
here you my Last chance
i just want you to change only
ServerName,User,Pass & file name
and try it
Thanks m8 !
zip and reattach
here you m8
Thanks
worked fine to my ftp server
ph.westconn.com.au/piter.txt