2

使用 FtpWebRequest 类并分配 BackgroundWorker 来上传文件。并且工作正常,但现在我想从目录及其子目录上传文件。

我创建了一个名为“FtpUploading”的组件,其中定义了私有 void 函数“FtpUploading_DoWork”

同样应该从 Windows 窗体应用程序调用...如果目录中有单个文件,则为工作文件,但如果有多个文件和子目录...它将不起作用。

    private void FtpUploading_DoWork(object sender, DoWorkEventArgs e)
    {
            BackgroundWorker bw = sender as BackgroundWorker;
            FtpSettings ftpDet = e.Argument as FtpSettings;

            string UploadPath = String.Format("{0}/{1}{2}", ftpDet.Host, ftpDet.TargetFolder == "" ? "" : ftpDet.TargetFolder + "/", Path.GetFileName(ftpDet.SourceFile));
            if (!UploadPath.ToLower().StartsWith("ftp://"))
                UploadPath = "ftp://" + UploadPath;
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(UploadPath);
            request.UseBinary = true;
            request.UsePassive = ftpDet.Passive;
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(ftpDet.Username, ftpDet.Password);

            long FileSize = new FileInfo(ftpDet.SourceFile).Length;
            string mFileName = new FileInfo(ftpDet.SourceFile).Name;
            string FileSizeDescription = GetFileSize(FileSize);
            int ChunkSize = 4096, NumRetries = 0, MaxRetries = 50;
            long SentBytes = 0;

            byte[] Buffer = new byte[ChunkSize];

            using (Stream requestStream = request.GetRequestStream())
            {
                using (FileStream fs = File.Open(ftpDet.SourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    int BytesRead = fs.Read(Buffer, 0, ChunkSize);  
                    while (BytesRead > 0)
                    {
                        try
                        {
                            if (bw.CancellationPending)
                                return;

                            requestStream.Write(Buffer, 0, BytesRead);
                            SentBytes += BytesRead;

                            string SummaryText = String.Format(mFileName + " => Transferred {0} / {1}", GetFileSize(SentBytes), FileSizeDescription);
                            bw.ReportProgress((int)(((decimal)SentBytes / (decimal)FileSize) * 100), SummaryText);
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine("Exception: " + ex.ToString());
                            if (NumRetries++ < MaxRetries)
                            {
                                fs.Position -= BytesRead;
                            }
                            else
                            {
                                throw new Exception(String.Format("Error occurred during upload, too many retries. \n{0}", ex.ToString()));
                            }
                        }
                        BytesRead = fs.Read(Buffer, 0, ChunkSize);  
                    }
                }
            }
            using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
                System.Diagnostics.Debug.WriteLine(String.Format("Upload File Complete, status {0}", response.StatusDescription));
        }


    public static string GetFileSize(long numBytes)
    {
        string fileSize = "";

        if (numBytes > 1073741824)
            fileSize = String.Format("{0:0.00} Gb", (double)numBytes / 1073741824);
        else if (numBytes > 1048576)
            fileSize = String.Format("{0:0.00} Mb", (double)numBytes / 1048576);
        else
            fileSize = String.Format("{0:0} Kb", (double)numBytes / 1024);

        if (fileSize == "0 Kb")
            fileSize = "1 Kb";  // min.                         
        return fileSize;
    }

//调用函数

        private void recursiveDirectory(string dirPath, string uploadPath)
    {
        string[] files = Directory.GetFiles(dirPath, "*.*");
        string[] subDirs = Directory.GetDirectories(dirPath);
        foreach (string file in files)
        {
            if (this.ftpUploading1.IsBusy)
            {
               // this.ftpUploading1.CancelAsync();
               // this.btnFtp.Text = "Upload";
                Thread.Sleep(50);
                break;
            }
            else
            {
                ftpSet.TargetFolder = uploadPath;
                ftpSet.SourceFile = file;
                this.toolStripProgressBar1.Visible = true;
                this.ftpUploading1.RunWorkerAsync(ftpSet);
                this.btnFtp.Text = "Cancel";
            }
        }
        foreach (string subDir in subDirs)
        {
            ftpClient.createDirectory(uploadPath + "/" + Path.GetFileName(subDir));
            recursiveDirectory(subDir, uploadPath + "/" + Path.GetFileName(subDir));
        }
    }
4

1 回答 1

0

您在启动 aync 任务后立即检查IsBusy,如果是true,则打破几乎总是如此的循环。这是怎么回事。

删除该条件将导致您在评论中提到的错误。因此,您应该等待任务完成,或者在FtpUploading_DoWork.

第三个(也是最好的)选项是使用Task. Task支持ContinueWith()这正是您所需要的。

我将在这里解释第一个解决方案,因为它只需要对现有代码进行很少的更改。这也只是为了让您了解同步过程。您可能需要进一步完善代码。

你需要一个WaitHandle,FtpUploading_DoWork来告诉它什么recursiveDirectory时候完成它的工作。我们将ManualResetEvent用于这件事。一旦您Reset使用 ManualResetEvent,它将等待调用,WaitOne直到另一个线程调用Set相同的 ManualResetEvent。

所以回去工作,添加一个ManualResetEvent到你的班级:

System.Threading.ManualResetEvent waitForUpload = new 
                               System.Threading.ManualResetEvent(false);

更改第一个for内部recursiveDirectory

    foreach (string file in files)
    {
        ftpSet.TargetFolder = uploadPath;
        ftpSet.SourceFile = file;
        this.toolStripProgressBar1.Visible = true;

        waitForUpload.Reset();
        this.ftpUploading1.RunWorkerAsync(ftpSet);
        this.btnFtp.Text = "Cancel";

        waitForUpload.WaitOne();
        while (this.ftpUploading1.IsBusy)
        {
            Thread.Sleep(100); // This is for the case if the WaitHandle is Set
                               // but the BackgroundWorker is still busy, 
                               // which should not take so long
        }
    }

并改变FtpUploading_DoWork

private void FtpUploading_DoWork(object sender, DoWorkEventArgs e)
{
    try
    {
         // Existing code inside the method
    }
    finally
    {
        waitForUpload.Set();
    }
}
于 2014-07-27T22:46:30.807 回答