Tuesday, June 22, 2010

Music Express Physics

How to download or download a file from an FTP server vb.Net

The previous article gave an example in which our system company aims to control all the files that our users are sending to each other and in which we had to we, as programmers, to see how how to upload or send a file to an FTP server in vb.net (or otherwise We said goodbye to the job), now I Decoding the code to its counterpart: download or download a file. As
we had stated in another topic, here are some additional recommendations:


* Having a good internet line, and less quick to download (or slower) to upload a file to a server, this consideration is very important because occasion could fail if the timeout expires for a connection to the FTP that is usually to have a pessimistic line.
* required fields have in our DB (SQL Server assume) for it to be effective to control such transfers of files, the data as the file name, user name remitenete and destination, time and date of receipt will be the keys to good control.
* Well, needless to say that they have have an FTP server where you store and where "jalaran" files to each user's PC to access them from anywhere in the world and from any era of a very distant future.

This is the code example in Visual Basic 2008

Where path filename would come to be the folder on your hard drive where the downloaded file will be saved and could be "c: \\ downloads \\ nombre_original_segun_BD.doc" Filename and the name which is stored in the FTP server, if we look good parameters would have no trouble getting the file to which you want to access each file and supposedly in the FTP server is registered in Database, here is the importance of keeping the data that are related to the file.


Public Sub DescargarArchivoFTP (ByVal path filename As String , ByVal FileName As String )

Dim LocalFiles As String = path filename

remotefile Dim As String Filename =

Const host As String = "ftp://xxx.xx.xx.xxx/archivos/" 'name of the folder in our server FTP where are the files you want to download

'put the username and password to access the respective server, if it does not possess, let alone the quotes, I mean " "

Const username As String = "user"

Const password As String = "password"

Dim URI As String = host & remotefile 'full name of the file path

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

ftp.Credentials = New _

System.Net.NetworkCredential (username, password)

ftp.KeepAlive = False

ftp.UseBinary = True

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

Try

Using response As System.Net.FtpWebResponse = _

CType (ftp.GetResponse, System.Net.FtpWebResponse)

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

Do

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

fs.Write(buffer, 0, read)

Loop Until read = 0

responseStream.Close()

fs.Flush()

fs.Close()

End Using

responseStream.Close()

End Using

response.Close()

End Using

Catch ex As Exception

MsgBox (ex.Message)

End Try

End Sub

0 comments:

Post a Comment