Remove The Cache Problem With FTP


By Umapathy Kaliaperumal
Printer Friendly Version
  

This function solves the cache problem mostly faced upon downloading files from ftp in .net 2.0. It bypasses the default downlaod.



Public Function SaveReadMeFile(ByVal host As String, ByVal ftpusername As String, ByVal ftppassword As String, ByVal localpath As String)

Dim cp As New Cache.RequestCachePolicy(RequestCacheLevel.NoCacheNoStore)

'Create a request

Dim URI As String = host

Dim ftp As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(URI), System.Net.FtpWebRequest)

'Set the credentials

ftp.Credentials = New System.Net.NetworkCredential(ftpusername, ftppassword)

'Turn off KeepAlive (will close connection on completion)

ftp.KeepAlive = False

'we want a binary

ftp.UseBinary = False

'Define the action required (in this case, download a file)

ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile

ftp.CachePolicy = cp

'If we were using a method that uploads data e.g. UploadFile

'we would open the ftp.GetRequestStream here an send the data

'Get the response to the Ftp request and the associated stream

Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)

Using responseStream As IO.Stream = response.GetResponseStream

'loop to read & write to file

Using fs As New IO.FileStream(localpath, IO.FileMode.Create)

Dim buffer(2047) As Byte

Dim read As Integer = 0

Do

read = responseStream.Read(buffer, 0, buffer.Length)

fs.Write(buffer, 0, read)

Loop Until read = 0 'see Note(1)

responseStream.Close()

fs.Flush()

fs.Close()

End Using

responseStream.Close()

End Using

response.Close()

End Using

Return True

End Function

 

 

'''how to use the function

 

 

Dim tmp As Boolean = False

tmp = Me.SaveReadMeFile(vsFtpZipPath & vguid & "/abc.xml", vftpUsername, vftpPassword, vsLocalZipPath & "abc.xml")


 




button
 
Article Discussion: a function to remove the cache problem with ftp
Umapathy Kaliaperumal posted at 19-Jun-08 09:09
Original Article