[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1591
  • Last Modified:

FTP File in ASP.net

I am trying to send a single file from my website to another server via FTP, reference the code here:

Here is the code converted to vb.net for my needs:
Public Sub uploadFileUsingFTP(ByVal CompleteFTPPath As String, ByVal CompleteLocalPath As String, Optional ByVal UName As String = "", Optional ByVal PWD As String = "")

            Dim ftp As FtpWebRequest = FtpWebRequest.Create(CompleteFTPPath)
            ftp.Credentials = New NetworkCredential(UName, PWD)
            ftp.UseBinary = True
            ftp.Method = WebRequestMethods.Ftp.UploadFile
            ftp.KeepAlive = False

            Dim buff As Byte()
            Using f As FileStream = DirectCast(File.OpenRead(CompleteLocalPath), FileStream)
                buff = New Byte(f.Length - 1) {}
                f.Read(buff, 0, buff.Length)
            End Using

            Using stream As Stream = ftp.GetRequestStream()
                stream.Write(buff, 0, buff.Length)
            End Using

        End Sub

When I try to run this code I get the following error:
Unable to cast object of type 'System.Net.HttpWebRequest' to type 'System.Net.FtpWebRequest'
on this line of code:
Dim ftp As FtpWebRequest = FtpWebRequest.Create(CompleteFTPPath)

Why?  Anyone?
  • 2
1 Solution
what is the value of the CompleteFTPPath you are passing to the function?
My guess is that this path is a HTTP url
alivemediaAuthor Commented:
ummm, yep.

DOH!!!!  I just had to change the http to ftp!

thanks for clueing me in!

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now