Click here to Skip to main content
15,884,059 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
i want to upload a file from fileupload control but insted of using http i want to use ftp... can any one help me out? thanks in advance...
Posted

Hi,

Me too came across same situation, and bit of R&D leads me to the following solution.
On a simple form there is a FileUpload control (say FileUpload1) and a Button Control (say Button1). On Click event of button write following code

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim f as bool = Upload(FileUpload1.FileBytes, FileUpload1.FileName, "abcftp", "abc@ftp", "ftp://ftp.someftp.com")
    Response.Write(f = True, "Uploaded Successfully", "Unable to Upload")
End Sub


Upload() function does all the magic
 Public Function Upload(FileByte() As Byte, FileName As String, ftpUserID As String, ftpPassword As String, ftpURL As String) As Boolean
    Dim retValue As Boolean = False
    Try
        Dim ftpFullPath As String = ftpURL + "/" + FileName
        Dim ftp As FtpWebRequest = FtpWebRequest.Create(New Uri(ftpFullPath))
        ftp.Credentials = New NetworkCredential(ftpUserID, ftpPassword)
        ftp.KeepAlive = True
        ftp.UseBinary = True
        ftp.Method = WebRequestMethods.Ftp.UploadFile
        Dim ftpStream As Stream = ftp.GetRequestStream()
        ftpStream.Write(FileByte, 0, FileByte.Length)
        ftpStream.Close()
        ftpStream.Dispose()
        retValue = True
    Catch ex As Exception
        Throw ex
    End Try
    Return retValue
End Function


I hope this will help.
Thanks.
 
Share this answer
 
Comments
Jose Alberto Lujan Huachhuaco 17-Nov-15 14:49pm    
Dim f as bool ?
 
Share this answer
 
Comments
Member 11073820 22-Dec-15 7:22am    
THANKS IT IS WORKING FINE

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