Solved

how to upload a file to FTP server?

Posted on 2011-09-17
6
304 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
  • 4
  • 2
6 Comments
 
LVL 5

Expert Comment

by:Eduardo Goicovich
Comment Utility
>> 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
Comment Utility
The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
0
 
LVL 5

Expert Comment

by:Eduardo Goicovich
Comment Utility
check the upload ftp path for white space between "/"
be sure that you have write access to destination directory
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:Rawasi
Comment Utility
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
Comment Utility
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
Comment Utility
the solution was as i mentioned, the slashes must be doubled thx
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now