Solved

how to upload a file to FTP server?

Posted on 2011-09-17
6
307 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
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
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!

 
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

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!

Question has a verified solution.

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

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 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