Jerry Miller
asked on
ASP.Net upload file via FTP
I have a requirement to give the users the ability to upload files for storage. I cannot store any files on the web server. We have access to an FTP server on which we store any uploaded files. I am using the below code to perform the function and it works fine on my localhost and stores the file on the FTP server as it should.
When I move it to the web server, generates an error "Could not find a part of the path" and it gives the path on the local box. The file is in that path, but it doesn't seem like the code can access it. How can I fix this?
Thanks,
Jerry
When I move it to the web server, generates an error "Could not find a part of the path" and it gives the path on the local box. The file is in that path, but it doesn't seem like the code can access it. How can I fix this?
Thanks,
Jerry
If FileUpload1.HasFile Then
Dim fileExt As String = System.IO.Path.GetExtension(FileUpload1.FileName)
Dim submitDate As Date = DateTime.Now.ToString()
Dim filepath = FileUpload1.PostedFile.FileName
Dim serverName As String = "servername"
Dim ID As String = "FTPID"
Dim password As String = "FTPpassword"
Dim nc As New System.Net.NetworkCredential(ID, password)
Dim cc As New System.Net.CredentialCache()
Dim strFTPFilePath As String = "ftp://" & serverName & "/Uploads" & "/" & FileUpload1.FileName
Dim reqObj As FtpWebRequest = DirectCast(FtpWebRequest.Create(New Uri(strFTPFilePath)), FtpWebRequest)
reqObj.Method = WebRequestMethods.Ftp.UploadFile
reqObj.Credentials = nc
reqObj.UseBinary = True
Dim stream As FileStream = File.OpenRead(filePath)
Dim buffer As Byte() = New Byte(stream.Length - 1) {}
stream.Read(buffer, 0, buffer.Length)
stream.Close()
'Upload file
Dim reqStream As Stream = reqObj.GetRequestStream()
reqStream.Write(buffer, 0, buffer.Length)
reqStream.Close()
Dim ftpWebResponse As FtpWebResponse = reqObj.GetResponse()
ftpWebResponse.Close()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Glad to help :-)
ASKER
Open in new window