Solved

how to upload a file to FTP server?

Posted on 2011-09-17
6
308 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Independent Software Vendors: 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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

695 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