简体   繁体   English

C# Amazon S3 上传带有进度条的文件

[英]C# Amazon S3 Upload file with progressbar

I have one class function for upload a file to S3:我有一个用于将文件上传到 S3 的类函数:

    public void UploadFile(string filePath, UploadProgress uploadProgress)
    {
        TransferUtilityUploadRequest uploadRequest;
        TransferUtility fileTransferUtility;

        try
        {
            mClient = new AmazonS3Client(Amazon.RegionEndpoint.EUWest1);

            uploadRequest =
                new TransferUtilityUploadRequest
                {
                    BucketName = BUCKETNAME,
                    FilePath = filePath
                };

            uploadRequest.UploadProgressEvent += new EventHandler<UploadProgressArgs>(uploadProgress);

            fileTransferUtility = new TransferUtility(mClient);
            fileTransferUtility.Upload(uploadRequest);
        }
        catch (AmazonS3Exception s3Exception)
        {
            throw s3Exception;
        }
    }

From the form class, I have a function for update the progressbar:在表单类中,我有一个更新进度条的函数:

    private void UploadFile_ProgressBar(object sender, UploadProgressArgs e)
    {
        int pctProgress = (int)(e.TransferredBytes * 100 / e.TotalBytes);
        progressBarUpload.Value = pctProgress;
        progressBarUpload.Invalidate();

        Console.WriteLine(e.TransferredBytes + " / " + e.TotalBytes + Environment.NewLine);            
    }

And the call to upload file is:上传文件的调用是:

    mS3.UploadFile(fileToUpload, UploadFile_ProgressBar);

The console show the progress but the progressbar no works.控制台显示进度,但进度条不起作用。 What am I doing wrong?我究竟做错了什么?

Thanks in advance!提前致谢!

I believe you are creating a Windows Form application with your progressbar. 我相信您正在使用进度条创建Windows Form应用程序。 So, check this out: http://www.dotnetperls.com/progressbar 因此,请查看以下内容: http : //www.dotnetperls.com/progressbar

Also, I saw you are using the .Invalidate Method of Progressbar. 另外,我看到您正在使用Progressbar的.Invalidate方法。 As documentation says, this method does force a synchronous paint, without the Update method. 如文档所述,此方法确实强制执行同步绘制,而没有Update方法。 Try it out. 试试看。

Calling the Invalidate method does not force a synchronous paint; 调用Invalidate方法不会强制执行同步绘制。 to force a synchronous paint, call the Update method after calling the Invalidate method. 要强制执行同步绘制,请在调用Invalidate方法之后调用Update方法。 When this method is called with no parameters, the entire client area is added to the update region. 当不带任何参数调用此方法时,整个客户区将添加到更新区域。

https://msdn.microsoft.com/en-us/library/598t492a(v=vs.100).aspx https://msdn.microsoft.com/zh-CN/library/598t492a(v=vs.100).aspx

Cheers! 干杯!

The Problem is That You Can not access member of System.Windows.Forms.Control from different thread than UI thread will cause cross-thread exception.问题是你不能从与 UI 线程不同的线程访问 System.Windows.Forms.Control 的成员会导致跨线程异常。 There is only one thread (UI thread), that is allowed to access System.Windows.Forms.Control and its subclasses members.只有一个线程(UI 线程)允许访问 System.Windows.Forms.Control 及其子类成员。

To Solve This You have to use the Invoke Method like this:要解决这个问题,您必须像这样使用 Invoke 方法:

this.BeginInvoke(new Action(() =>
{
    this.progressBarUpload.Value = pctProgress;
}));

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

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