Solved

how to upload a file to FTP server?

Posted on 2011-09-17
6
305 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Check my code out(2) 2 54
Runtime Error 2 28
Coding C# in Linux 8 34
Code works but it's slow 24 44
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

13 Experts available now in Live!

Get 1:1 Help Now