We help IT Professionals succeed at work.
Get Started

ASP.NET using VB FTP Upload

Last Modified: 2013-11-29
I wrote a function that uploads images to my web server.  The function works fine when I execute the code, but when I post the page and try to use the function through the website, I receive this error:
System.IO.FileNotFoundException: Could not find file 'c:\windows\system32\inetsrv\myimagename.jpg'. File name: 'c:\windows\system32\inetsrv\myimagename.jpg' at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) at System.IO.File.ReadAllBytes(String path) at SiteFTPFunction.FTPUpload(String FTPDestination, String LocalCopy, String strUserName, String strFileName) in C:\Domains\mywebsite.net\wwwroot\App_Code\SiteFTPFunction.vb:line 24
Public Function FTPUpload(ByVal FTPDestination As String, ByVal LocalCopy As String, ByVal strUserName As String, ByVal strFileName As String) As String
        Dim strResult As String = ""
        Dim filePath As String = ""
        Dim newFileName As String = ""

            Dim clsRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create(ftpDirectory & FTPDestination), System.Net.FtpWebRequest)
            clsRequest.Credentials = New System.Net.NetworkCredential(ftpUserName, ftpPassword)
            clsRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile

            Dim bFile() As Byte = System.IO.File.ReadAllBytes(LocalCopy)
            Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
            clsStream.Write(bFile, 0, bFile.Length)


            strResult = RecordFileUpload(strUserName, strFileName, LocalCopy, FTPDestination)
            strResult = "Success"

        Catch ex As Exception

            strResult = "Error"
            strResult = ex.ToString

        End Try

        Return strResult

    End Function

Open in new window

Watch Question
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE