[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

how to upload a file to FTP server?

Posted on 2011-09-17
6
Medium Priority
?
311 Views
Last Modified: 2012-05-12
i have a web application i need to make the user able to upload large files using FTP upload, so I've
created a FTP account on the host server, I've this code snippet but there are some problems,i don't have any experience with FTP upload any help ?
public void FtpFileUpload(string filename, System.Drawing.Image img)
        {
            //host name
            string ftphost = "ip address";
            //file path
            string ftpfilepath = @"/example.com/some folder/" + filename;
            string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
            FtpWebRequest request = FtpWebRequest.Create(new Uri(ftpfullpath)) as FtpWebRequest;
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential("username", "password@example.com");
            request.KeepAlive = false;
            request.UsePassive = false;
            request.UseBinary = true;
            FileStream stream = File.OpenRead(@"c:\imagesTemp\" + filename);
            request.ContentLength = stream.Length;
            byte[] buffer1 = new byte[stream.Length];
            stream.Read(buffer1, 0, buffer1.Length);
            stream.Close();
            Stream ftpstream = request.GetRequestStream();
            ftpstream.Write(buffer1, 0, buffer1.Length);
            ftpstream.Close();
        }

Open in new window

0
Comment
Question by:Rawasi
[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
  • 4
  • 2
6 Comments
 
LVL 5

Expert Comment

by:Eduardo Goicovich
ID: 36554107
>> request.ContentLength = stream.Length;

afted allocate stream in buffer, get buffer size

StreamReader sourceStream = new StreamReader((@"c:\imagesTemp\" + filename);
            byte [] buffer1 = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            sourceStream.Close();
            request.ContentLength = buffer1 .Length;

and get response from server, after upload

FtpWebResponse response = (FtpWebResponse)request.GetResponse();
   
            Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);
   
            response.Close();
0
 
LVL 1

Author Comment

by:Rawasi
ID: 36554127
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
0
 
LVL 5

Expert Comment

by:Eduardo Goicovich
ID: 36554164
check the upload ftp path for white space between "/"
be sure that you have write access to destination directory
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Rawasi
ID: 36554173
there are no white spaces and i have write permissions to the home directory and all the sub directories
can you please give me a valid code snippet and ill compare it with the one i have ?  
0
 
LVL 1

Accepted Solution

by:
Rawasi earned 0 total points
ID: 36554397
the error in the code was the slashes string ftpfilepath = "//example.com//some folder//" + filename;
OMG I've spent the last two days for this !! :S


thanks for your help
0
 
LVL 1

Author Closing Comment

by:Rawasi
ID: 36578996
the solution was as i mentioned, the slashes must be doubled thx
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

656 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