如何使用后台工人制作FTP上传功能

本文关键字:FTP 功能 何使用 后台 | 更新日期: 2023-09-27 18:36:03

这是我的代码

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");
        }
    }

当我单击按钮6时,我收到的消息是"Workker运行!!"之后什么都没有,***我的光标仍然是默认的(从不改变!!,为什么?),我是后台的超级新手工人对不起:(请帮帮我....

如何使用后台工人制作FTP上传功能

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

原因是,backgroundWorker1.RunWorkerAsync();调用将在另一个线程中运行工作线程。因此,下一行将立即执行。

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

编辑:

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

将标签文本设置为"正在上传...."的示例:

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

您所要做的就是这样的事情(对代码进行一些调整)

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