lunes, 27 de octubre de 2008

Subir un archivo por ftp en asp.net 2.0

 

creamos la funcion uploadFileUsingFTP lo cual sera usado

uploadfileusingftp("ruta del ftp","direccion completa del archivo ","usuario","password")

 

Public Sub uploadFileUsingFTP(ByVal CompleteFTPPath As String, ByVal CompleteLocalPath As String, Optional ByVal UName As String = "", Optional ByVal PWD As String = "")

'Create a FTP Request Object and Specfiy a Complete Pat

'creando un objeto request ftp especificando la ruta completa del ftp

Dim reqObj As FtpWebRequest = WebRequest.Create(CompleteFTPPath)

llamando el metodo fileupload del objeto que creamos

reqObj.Method = WebRequestMethods.Ftp.UploadFile

'si el ftp requiere usuario y clave se configura

reqObj.Credentials = New NetworkCredential(UName, PWD)

on objeto filestream leera el archivo que se va asubir

Dim streamObj As FileStream = File.OpenRead(CompleteLocalPath)

'se guarda en un buffer

Dim buffer(streamObj.Length) As Byte

ahora se lee el archivo en el buffer

streamObj.Read(buffer, 0, buffer.Length)

se cierra el stream

streamObj.Close()

streamObj = Nothing

se sube un archivo en caso no existe

reqObj.GetRequestStream().Write(buffer, 0, buffer.Length)

reqObj = Nothing

End Sub

No hay comentarios: