Grabbing individual files via FTP in VB 2005 / VB
July 9, 2008 at 10:21 pm | In Non Insteon Programming | Leave a CommentTags: caller id, ftp client, ftp in vb, FtpWebRequest, Insteon, system.net.sockets ftp example, System.Net.WebRequestMethods.Ftp.DownloadFile, X10
For my voice mail / Caller-ID function I placed the name / number of the person in a database but also I have the name of my contact (joe.png, luis,jpg etc), After Ive retrieved the information, I FTP back to the server to grab the image file
Here is how its done.
For the example;
FTP server is at 192.168.0.101
username is :webuser
password is :password
File to get: <variable called ImageName> in a folder called Images
Location to save: c:\images\<ImageName>
The default folder on my server have a folder called images/
On the server im running FileZilla FTP Server (Free)
'------------------------------------------------------------------------------
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Public Function GetFTP(ByVal ImageName As String)
Dim localFile As String = "c:\images\" + ImageName
Dim remoteFile As String = "/images/" + ImageName
Dim host As String = "ftp://192.168.0.101"
Dim username As String = "webuser"
Dim password As String = "password"
'1. Create a request: must be in ftp://hostname format,
Dim URI As String = host & remoteFile
Dim ftp As System.Net.FtpWebRequest = _
CType(FtpWebRequest.Create(URI), FtpWebRequest)
'2. Set credentials
ftp.Credentials = New _
System.Net.NetworkCredential(username, password)
'3. Settings and action
ftp.KeepAlive = False
'we want a binary transfer, not textual data
ftp.UseBinary = True
'Define the action required (in this case, download a file)
ftp.UsePassive = False
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
'Loop to read & write to file
Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)
'-----Setup Response stream to grab the FileStream aka Data and send it to a file using the FileStream object.
Using responseStream As IO.Stream = response.GetResponseStream
Using fs As New IO.FileStream(localFile, IO.FileMode.Create)
Dim buffer(2047) As Byte
Dim read As Integer = 0
'-----Once Data is detected in the ResponseStream Loop until empty,
Do
read = responseStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read) '----- Write the 'read' variable into the file
Loop Until read = 0
responseStream.Close()
fs.Flush()
fs.Close()
End Using
responseStream.Close()
End Using
response.Close()
End Using
Exit function
PRESTO!
No Comments Yet »
RSS feed for comments on this post. TrackBack URI
Leave a comment
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.