Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Dear All,

I want to move some files(word, excel, & text files) in my local drive to remote server location (with specified location) through web application. I google about this, i got some inbuilt dlls. but i don't want use dlls, how to write programatically.
VB
'Settings required to establish a connection with the server

Public Function UploadToServer(ByVal FileName As String) As Boolean

Try


Dim ftpRequest As FtpWebRequest = CType(FtpWebRequest.Create(New Uri("ftp://" + FileName)), FtpWebRequest)

ftpRequest.Method = WebRequestMethods.Ftp.UploadFile
ftpRequest.Proxy = Nothing
ftpRequest.UseBinary = True
ftpRequest.EnableSsl = True
ftpRequest.KeepAlive = True


ftpRequest.Credentials = New NetworkCredential("test", "test1234")


'Selection of file to be uploaded
Dim ff As FileInfo = New FileInfo("D:\Sample2\" + FileName)

Dim fileContents() As Byte = New Byte((ff.Length) - 1) {}

Dim fr As FileStream = ff.OpenRead
fr.Read(fileContents, 0, Convert.ToInt32(ff.Length))

'Dim no As Integer = ftpRequest.ServicePoint.CurrentConnections
'ftpRequest.ServicePoint.ConnectionLimit = 10

ftpRequest.Timeout = 999999999
ftpRequest.ReadWriteTimeout = 999999999

Dim writer As Stream = ftpRequest.GetRequestStream
writer.Write(fileContents, 0, fileContents.Length)    ' // Am getting error like The underlying connection war closed.  and The remote certificate is invalid according                                                                                         ' //to the validation procedure.

writer.Close()

fr.Close()
'ftpRequest.Abort()

flag = "Y"


Return True

Catch ex As Exception
'Return False
flag = "N"
Throw
'Finally

End Try

End Function
Posted
Updated 5-Sep-13 18:26pm
v3
Comments
Dave Kreskowiak 5-Sep-13 9:12am    
Confused... A "web application" normally refers to something like an ASP.NET project, which all the code runs server-side, not in the browser. Your code doesn't make any sense from that standpoint.

You're going to have to explain your setup for this app and what code you expect to run where.
Maria Vinod 6-Sep-13 0:24am    
Dear Dave,
My requirement is to move files my local system to ftp server location, this code is perfectly working for ftp server location. But when am moving these files to sftp server then getting error like "The underlying connection was closed: The server committed a protocol violation". Please help how to move files to SFTP server location ? I hope you understand my problem.
Dave Kreskowiak 6-Sep-13 0:35am    
You're going to have to find a 3rd party library to do SFTP. The FtpClient class does not support SFTP.

Google for ".NET SFTP library".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900