繁体   English   中英

无法从C#(WPF)FTP上传文件

[英]Can not upload file from C# (WPF) FTP

我已经使用Winforms在C#中创建了一个小型ftp程序,但是当我尝试在wpf中创建它时,它向我显示错误“值不能为空。参数名称:fileName”,该代码在winform中可以正常工作。 这是代码:

public partial class Ftp : UserControl
{
    string filePath;
    public Ftp()
    {
        InitializeComponent();
    }
    //Clear TextBox when clicked
    #region textBox clear
    public void TextBox_GotFocus(object sender, RoutedEventArgs e)
    {
        //TextBox tb = (TextBox)sender;
        //tb.Text = string.Empty;
        //tb.GotFocus -= TextBox_GotFocus;
    }
    #endregion

    //Open File Dialog
    private void browser_click(object sender, RoutedEventArgs e)
    {
        #region File Dialog
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
        dlg.CheckFileExists = true;
        dlg.DefaultExt = ".txt";
        dlg.Filter = "JPEG Files (*.txt)|*.txt|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
        dlg.FileName = "";

        Nullable<bool> result = dlg.ShowDialog();

        if (result == true)
        {
            // Open document 
            string filename = dlg.FileName;
            pathtext.Text = filename;
        }
        #endregion
    }

    //Upload the File to FTP
    private void Upload_click(object sender, RoutedEventArgs e)
    {
        #region Upload files
        if (filePath == "")
        {
            MessageBox.Show("Please Select The File To Upload..");
        }
        else
        {
            browser.IsEnabled = false;
            try
            {
                FileInfo fileInfo = new FileInfo(filePath);
                string fileName = fileInfo.Name.ToString();
                WebRequest requestFTP = WebRequest.Create("ftp://" + hosttext.Text + "/" + fileName);
                requestFTP.Credentials = new NetworkCredential(usertext.Text, passtext.Password);
                requestFTP.Method = WebRequestMethods.Ftp.UploadFile;
                FileStream fStream = fileInfo.OpenRead();
                int bufferLength = 2048;
                byte[] buffer = new byte[bufferLength];

                Stream uploadStream = requestFTP.GetRequestStream();
                int contentLength = fStream.Read(buffer, 0, bufferLength);

                while (contentLength != 0)
                {
                    uploadStream.Write(buffer, 0, contentLength);
                    contentLength = fStream.Read(buffer, 0, bufferLength);
                }
                uploadStream.Close();
                fStream.Close();
                requestFTP = null;
                MessageBox.Show("File Uploading Is SuccessFull...");
            }
            catch (Exception ep)
            {
                MessageBox.Show("ERROR: " + ep.Message.ToString());

            }
            browser.IsEnabled = true;
            filePath = "";
            hosttext.Text = usertext.Text = passtext.Password = pathtext.Text = "";
        }
        #endregion

    }
}

您永远不会设置filePath变量,因此它为null。 browser_click您需要执行以下操作

if (result == true)
{
    // Open document 
    string filename = dlg.FileName;
    filePath = filename;           //added this line
    pathtext.Text = filename;
}

另外,您认为您已经在此处处理了无效的字符串:

if (filePath == "")

但是那时filePath = null

而是使用if (string.IsEmptyOrNull(filePath))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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