Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public static void UpLoadImage(string source)
        {
            
            try
            {
                string filename = System.IO.Path.GetFileName(source);
               // string ftpfullpath = ftpurl;
                FtpWebRequest ftp = (FtpWebRequest)WebRequest.Create("ftp://192.168.0.41");
               // ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);

                MessageBox.Show(ftp.GetResponse().ToString());

                ftp.KeepAlive = true;
                ftp.UseBinary = true;
                ftp.Method = WebRequestMethods.Ftp.UploadFile;

                FileStream fs = File.OpenRead(source);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                fs.Close();

                Stream ftpstream = ftp.GetRequestStream();
                ftpstream.Write(buffer, 0, buffer.Length);
                ftpstream.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }




i want to upload image to the ftp server (IIS) but i try to upload i got this exceptions:

An unhandled exception of type 'System.Net.WebException' occurred in

--> The exceptions:The requested URI is invalid for this FTP command


How can i fix this?
Posted
Updated 23-Nov-14 23:02pm
v3
Comments
Herman<T>.Instance 24-Nov-14 4:39am    
On what line the exception occurs?
[no name] 24-Nov-14 4:40am    
What is the exception ?
deepgalley 24-Nov-14 4:49am    
you need to specify the port. This will take a default port. Assign a open port in your network like 587
Member 10525430 24-Nov-14 4:50am    
The exceptions:The requested URI is invalid for this FTP command
[no name] 24-Nov-14 10:11am    
Remove the try block and see where the error occurs. And step through your code with the debugger to see what your code is doing. Also try adding a / at the end of the ip and you should also be specifying a port. Does your FTP only allow Secure Connections?

Try something like this, pass your credentials to it:
private bool isConnectionValid(string url, string user, string password)
{
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential(user, password);
request.GetResponse();
}
catch(WebException ex)
{
return false;
}
return true;
}

1 solution

You have to add filename into Crete method...having just target folder will fail.

C#
FtpWebRequest ftp = (FtpWebRequest)WebRequest.Create("ftp://192.168.0.41/your_file.ext");



If this helps please take time to accept the solution. Thank you
 
Share this answer
 
Comments
BillWoodruff 24-Nov-14 7:03am    
The OP's code, as it is now, will not fail in the line where WebRequest.Create is called, but you are correct.
Member 10525430 25-Nov-14 4:52am    
Its not solve my problem. how can i close ftp authentication on code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900