Hello,
I have to develop a program that will allow a user to enter a url of a remote server and then copy files there.
I am not sure to get started with this, can anyone point me in the right direction.
Many thanks for any advice,
Steve
Printable View
Hello,
I have to develop a program that will allow a user to enter a url of a remote server and then copy files there.
I am not sure to get started with this, can anyone point me in the right direction.
Many thanks for any advice,
Steve
Server? What kind of server? On your local network? An ftp server? ("Server" just means it's a computer with a program that's looking for incoming requests on some port.)
Hello,
Maybe I should have been more specific it could be either a web server (port 80) or a ftp server (port 21).
Thanks for any ideas that you can give me.
Steve
You can use either the Inet control, Winsock control, or the Winsock API functions.
Files uploaded to a web server are usually done so via FTP. Otherwise they are usually posted to a PHP script via the HTTP POST method. You can do this with Inet pretty easily, there is a lot of code on the net and these forums for this.
Winsock gives you much more control over everything, but if all you want to do is upload files the easy way then Inet is probably your best bet.
you can use this function to upload a file with the Inet control via FTP. The server must be running as an FTP server in order for it to accept the files.
VB Code:
Private Sub Command1_Click() 'Upload the file Dim Server As String, Username As String, Password As String, LocalName As String, RemoteName As String Server = "ftp.somewhere.com" Username = "the182guy" Password = "password182" LocalName = App.Path & "\IP_Address.txt" 'give it the location of the file to upload RemoteName = "IP_Address.txt" 'once upload the file will be named this If UploadFile(Inet1, Server, Username, Password, Chr(34) & LocalName & Chr(34), RemoteName) = True Then MsgBox "Upload complete", vbInformation Else MsgBox "error file did not uplaod", vbExclamation End If End Sub Public Function UploadFile(Inet As Inet, ByVal HostName As String, ByVal Username As String, ByVal Password As String, ByVal LocalFileName As String, ByVal RemoteFileName As String) As Boolean 'This is the function used to upload the file Inet1.Protocol = icFTP 'set the inet to FTP protocol Inet1.RemoteHost = HostName$ 'set the server Inet1.Username = Username$ 'set username Inet1.Password = Password$ 'set password Inet1.Execute Inet1.URL, "Put " + LocalFileName$ + " " + RemoteFileName$ 'upload the file Do While Inet1.StillExecuting 'loop while waiting DoEvents Loop UploadFile = (Inet1.ResponseCode = 0) 'return boolean End Function
For servers supporting WebDAV (and probably Frontpage Server Extensions) there is another way.
This method uses the capabilities of ADO 2.5 or later and the OLEDB Provider for Internet Publishing. Windows 2000 and later come with this Provider, and installing IE 5.0 or later adds (or updates) it as needed.
While the documentation seems pretty good in general there is a dearth of working examples of uploading a file this way. I've attached a small example that tests out fine here. The web server WebDAV folder URL and file to send are hard-coded but these can easily be accepted from the user as well as User/PW.
This code should also work with servers that have Frontpage Server Extensions but I haven't tested it. Of course the security at the server must allow WebDAV writes to the target folder.
VB Code:
Private Sub Command1_Click() 'Write file "test.htm" from current directory 'to WebDAV folder TestDir on server BigDog 'with overwrite. Dim recDir As New ADODB.Record Dim stmData As New ADODB.Stream recDir.Open "test.htm", "URL=http://BigDog/TestDir", _ adModeReadWrite, _ adCreateNonCollection Or adCreateOverwrite, _ adDelayFetchStream, _ Text1(0).Text, _ Text1(1).Text Print "* Record opened to web server" With stmData .Type = adTypeBinary 'Sample is ANSI text but avoid adding BOM. .Open recDir, adModeWrite, adOpenStreamFromRecord Print "* Stream opened to web server" .LoadFromFile "test.htm" Print "* Local file loaded into Stream" .Close Print "* File saved to web server" End With recDir.Close Print "* Complete" Command2.SetFocus End Sub
Use Winsock or Inet.