Monday, June 21, 2010

Constant Prostate Pain

How to send a file to an FTP server in VB. Net

The issue is this: We charge an application or a module within our main system, which can be sending and receiving files (documents or whatever) which is contained in important information for the company to one or more users which will target just the file. For this will take into account sieguiente:

* Have a PC or FTP server where they were stored and accessed from anywhere in the world and at any time to the files.
* Have the appropriate fields in a table in our BD code indicating the user's file name (ejemplo.doc) etc.
* Having a good internet line
Eye
here and I will not detail that code used in vb.net and SQL Server to save the parameters in the database of the fields mentioned, because that will be defined for every programmer only show you an example of how to send a file from FTP:

Code: Where
OrigenArchivo
might be "c: \\ ejemplo.doc" and could be FileName "Archiv0_0001.doc" because I recommend using a correlative when you name the file on the FTP server as occasion may fail if the ship with a name of a file that already exists in the destination folder created on the FTP server, eh hence the importance of keeping the data name (ejemplo.doc) so that when receiving or downloading the file is saved to local disk with the original name.


Public Shared Sub EnviarArchivoFTP ( ByVal OrigenArchivo As String, ByVal FileName As String )

clsRequest Dim As System.Net.FtpWebRequest

Dim connection As Net.Sockets.TcpClient

clsRequest = DirectCast (System.Net.WebRequest.Create ( "ftp:// xxx.xx.xxx.xxx / files / filename) System.Net.FtpWebRequest)

clsRequest.Proxy = Nothing 'This assignment is important to work on Windows XP and by default this property is to be assigned to an http server which ocacionaria an error if we want to connect to an FTP site in Windows Vista and Seven have not had this problem.

clsRequest.Credentials = New System.Net.NetworkCredential ( "user" , "password" ) 'Username and password to access the FTP server, if not tubiese, leave quotes, osea "

clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

Try

bFile Dim () As Byte = System.IO. File.ReadAllBytes (origenArchivo)

Dim clsStream As System.IO.Stream = _

clsRequest.GetRequestStream()

clsStream.Write(bFile, 0, bFile.Length)

clsStream.Close()

clsStream.Dispose()

Catch ex As Exception

MsgBox (ex.Message & . "The file could not be sent, try later" )

End Try

End Sub

0 comments:

Post a Comment