We help IT Professionals succeed at work.

ASP.NET using VB FTP Upload

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

Somehow the path where it is trying to upload the file is of c:\windows\system32, and asp.net should not upload file to  the system32 directory.

Cross check the value in the FtpHost variable, it should be of your virtual directory configured.
your code may work fine on local pc.
Bu this will not work on server.
You are using FTP to upload the file. You cannot assume that file you are accessing will be on the server.

Try changing the path