简体   繁体   中英

FTP upload never throws an error issue

I've created an application that uploads files to a remote FTP server. If the credentials (address, username, password, etc.) are wrong I want to to throw an error. As of right now it never does. What is wrong with my code? When the credentials are correct it does successfully upload.

here is the class I am using:

 public void upload(string remoteFile, string localFile)
    {
        try
        {
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
            ftpStream = ftpRequest.GetRequestStream();
            FileStream localFileStream = File.OpenRead(localFile);
            byte[] byteBuffer = new byte[bufferSize];
            int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
            try
            {
                while (bytesSent != 0)
                {
                    ftpStream.Write(byteBuffer, 0, bytesSent);
                    bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
                }
            }
            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            localFileStream.Close();
            ftpStream.Close();
            ftpRequest = null;
        }
        catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        return;
    }

and where I'm calling the upload function in the form:

On button click =

    ftp ftpClient = new ftp(@"ftp://weburl.com/", "user", "password");
    UploadToFTP("testing/file.txt" , @"C:\file.txt");

    void UploadToFTP(string FTPfolder, string LocalFolder)
    {
        try
        {
            ftpClient.upload(FTPfolder, LocalFolder);
            Messagebox.Show("Uploaded Successfully!");
        }
        catch (Exception ex)
        {
            Messagebox.Show(ex.ToString());
        }
    }

From my comment if there is an error, its spitting it to the console, but isn't waiting for readkey, so the console is disappearing, you never catch another exception in your call because the catch in the function is handling the error. Inside the function replace Console.WriteLine with Messagebox.Show to see the caught errors

 public void upload(string remoteFile, string localFile)
    {
        try
        {
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
            ftpStream = ftpRequest.GetRequestStream();
            FileStream localFileStream = File.OpenRead(localFile);
            byte[] byteBuffer = new byte[bufferSize];
            int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
            try
            {
                while (bytesSent != 0)
                {
                    ftpStream.Write(byteBuffer, 0, bytesSent);
                    bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
                }
            }
            catch (Exception ex) { Messagebox.Show(ex.ToString()); }
            localFileStream.Close();
            ftpStream.Close();
            ftpRequest = null;
        }
        catch (Exception ex) { Messagebox.Show(ex.ToString()); }
        return;
    }

Rewrite your upload function is eating the exception.

I would rewrite your catch block this way:

        try
        {
            // ....
        }
        catch (WebException webEx)
        {
            string message = string.Empty;

            if (webEx.Response.GetType() == typeof(FtpWebResponse))
            {
                FtpWebResponse response = (FtpWebResponse)webEx.Response;
                message = string.Format("{0} - {1}", response.StatusCode, response.StatusDescription);
            } else
            {
                message = webEx.Message;
            }

            throw new Exception(message);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM