Monday, April 8, 2013

Upload To FTP with FileUpload control

Sometimes we come across a situation when we want get file from user through file upload and upload it to remote server through ftp.In this type of situation we can just create  a request stream variable and using that request stream we can write the contents of fileupload to remote server.For this we have to get the content of file upload control in bytes.Here is  a sample how i accomplish the same thing.Here spath is the path ftp path to remote server.ftpuser is user name and ftppassword is password to remote server.

Dim request As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(spath), System.Net.FtpWebRequest)
request.Credentials = New System.Net.NetworkCredential(ftpuser, ftppassword)
request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
Dim ftpStream As Stream = request.GetRequestStream()
ftpStream.Write(FileUpload1.FileBytes, 0, FileUpload1.FileBytes.Length)
ftpStream.Close()
ftpStream.Dispose()
Note:Don't forget to add following lines at the end of the code
ftpStream.Close()
ftpStream.Dispose()

No comments :

Post a Comment