Link to home
Start Free TrialLog in
Avatar of MeghanOBrien
MeghanOBrienFlag for United States of America

asked on

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 = ""

        Try
            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)

            clsStream.Dispose()
            clsStream.Close()

            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

ASKER CERTIFIED SOLUTION
Avatar of Ajay Sharma
Ajay Sharma
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Amandeep Singh Bhullar
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