如何使用backgroundWorker进行FTP上传功能

时间:2013-10-20 09:56:52

标签: c# .net upload ftp backgroundworker

这是我的代码

public bool FTPUploadFunct(string uploadto2, string newskinlocation2)
    {
        bool FTPUploadFunct = true;
        toolStripStatusLabel1.Text = "uploading....";
        MessageBox.Show("uploading");
        try
        {
            //delete old file
            FtpWebRequest requestFileDelete = (FtpWebRequest)WebRequest.Create(uploadto2);
            requestFileDelete.Credentials = new NetworkCredential("FTPUser20", "1234");
            requestFileDelete.Method = WebRequestMethods.Ftp.DeleteFile;

            FtpWebResponse responseFileDelete = (FtpWebResponse)requestFileDelete.GetResponse();

            //upload new file
            FtpWebRequest requestFTPUploader = (FtpWebRequest)WebRequest.Create(uploadto2);
            requestFTPUploader.Credentials = new NetworkCredential("FTPUser20", "1234");
            requestFTPUploader.Method = WebRequestMethods.Ftp.UploadFile;

            FileInfo fileInfo = new FileInfo(newskinlocation2);
            FileStream fileStream = fileInfo.OpenRead();

            int bufferLength = 2048;
            byte[] buffer = new byte[bufferLength];

            Stream uploadStream = requestFTPUploader.GetRequestStream();
            int contentLength = fileStream.Read(buffer, 0, bufferLength);

            while (contentLength != 0)
            {
                uploadStream.Write(buffer, 0, contentLength);
                contentLength = fileStream.Read(buffer, 0, bufferLength);
            }

            uploadStream.Close();
            fileStream.Close();

            requestFTPUploader = null;
        }
        catch (WebException ex)
        {
            FTPUploadFunct = false;
            String status = ((FtpWebResponse)ex.Response).StatusDescription;
            MessageBox.Show(status);
            int errorNumber = (int)ex.Status;
            if (errorNumber == 550)
            {
                MessageBox.Show("550");
            };
        }
        return FTPUploadFunct;
    }

我在

时使用它
private void button6_Click(object sender, EventArgs e)
    {
        Cursor.Current = Cursors.WaitCursor;
        uploadto = ("ftp://" + severip + ":21/" + "IMG/" + username + ".png");

        backgroundWorker1.RunWorkerAsync();
        Cursor.Current = Cursors.Default;
    }

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        MessageBox.Show("Workker run!!!");
        bool resualtUp = FTPUploadFunct(uploadto, newskinlocation);
        if (resualtUp == true)
        {
            MessageBox.Show("True");
        }
        else
        {
            MessageBox.Show("False");
        }
    }
当我点击button6时,我得到的消息是“Workker run !!!” 之后没什么,***我的光标仍然是默认的(永远不要改变!!!,为什么?),我是backgroundWorker的超级新手抱歉:( 请帮帮我....

2 个答案:

答案 0 :(得分:2)

一瞬间,光标变为等待光标。

原因是,backgroundWorker1.RunWorkerAsync();调用将在另一个线程中运行该worker。所以下一行会在之后立即执行。

解决方案是从Cursor.Current = Cursors.Default;方法移除button6_Click行,并将其添加到BackgroundWorker.RunWorkerCompleted事件中。

修改

要设置标签文本或使用控件执行任何操作(启用/禁用按钮等),请使用Form.Invoke()

将标签文本设置为“upload ....”的示例:

this.Invoke(() => {
    toolStripStatusLabel1.Text = "uploading....";
});

答案 1 :(得分:0)

大家所有你需要做的就是这样(对代码进行一些调整)

public Form1()
    {
        InitializeComponent();
        backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
        backgroundWorker1.WorkerReportsProgress = true;  
        backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
    }

    void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        this.Cursor = Cursors.Default; 
    }
 void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {

    }

在你的代码中你应该添加这行代码

while (contentLength != 0)
            {
                //here you  should do  
                backgroundWorker1.ReportProgress((int)(contentLength / fileInfo.Length));  
                uploadStream.Write(buffer, 0, contentLength);
                contentLength = fileStream.Read(buffer, 0, bufferLength);
            }


 Hope  this  help