Solved

ASP.Net upload file via FTP

Posted on 2013-01-09
3
929 Views
Last Modified: 2013-01-10
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

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

Open in new window

0
Comment
Question by:Jerry Miller
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 38761293
Between

Dim filepath = FileUpload1.PostedFile.FileName

and

Dim stream As FileStream = File.OpenRead(filePath)

I don't see any FileUpload1.PostedFile.SaveAs call to save the file. You need to save it locally before reading it.


But you do not need to do that as the PostFile.InputStream can be used to read the bytes directly

http://msdn.microsoft.com/en-us/library/system.web.httppostedfile.inputstream.aspx
0
 
LVL 18

Author Comment

by:Jerry Miller
ID: 38763758
I changed File.OpenRead(filepath) to FileUpload1.PostedFile.InputStream and it works like a charm. Thanks for the help CodeCruiser!

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 fStream As Stream = FileUpload1.PostedFile.InputStream
            Dim buffer As Byte() = New Byte(fStream.Length - 1) {}

            fStream.Read(buffer, 0, buffer.Length)
            fStream.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()

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38763787
Glad to help :-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question