Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用await async上载文件时,vb.net进度条值引发ArgumentOutOfRange异常_Vb.net_Progress Bar_Filesize_Upload Max Filesize - Fatal编程技术网

使用await async上载文件时,vb.net进度条值引发ArgumentOutOfRange异常

使用await async上载文件时,vb.net进度条值引发ArgumentOutOfRange异常,vb.net,progress-bar,filesize,upload-max-filesize,Vb.net,Progress Bar,Filesize,Upload Max Filesize,我无法更新ProgressBar值,我也尝试了 Convert.ToInt32(字节长度)。但是,它不起作用。 我正在使用(整数的)进度 “按一下按钮 Public Shared s3client As AmazonS3Client Public Shared myProgress As Progress(Of Integer) Public Shared Bytes As Double Public Shared myProgress As Progress(Of Integer) P

我无法更新ProgressBar值,我也尝试了
Convert.ToInt32(字节长度)
。但是,它不起作用。 我正在使用(整数的)进度

“按一下按钮



Public Shared s3client As AmazonS3Client
Public Shared myProgress As Progress(Of Integer)  
Public Shared Bytes As Double
Public Shared myProgress As Progress(Of Integer)
Public Shared bucketName As String = "S3BucketName"

Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button2.Click

 Dim AllowedFiles As List(Of String) = New List(Of String)

            Try
                Dim TheSize As Long
                Dim TotalSize As Long
                For Each file In AllowedFiles
                    TheSize = Long.Parse((My.Computer.FileSystem.GetFileInfo(file).Length))
                    TotalSize += TheSize
                Next

                Select Case TotalSize
                    Case Is >= 1099511627776
                        Bytes = CDbl(TotalSize / 1099511627776) 'TB
                    Case 1073741824 To 1099511627775
                        Bytes = CDbl(TotalSize / 1073741824) 'GB
                    Case 1048576 To 1073741823
                        Bytes = CDbl(TotalSize / 1048576) 'MB
                    Case 1024 To 1048575
                        Bytes = CDbl(TotalSize / 1024) 'KB
                    Case 0 To 1023
                        Bytes = TotalSize ' bytes
                    Case Else
                        Bytes = 0
                        'Return ""
                End Select

                ProgForm2.CPBar1.Value = 0
                ProgForm2.CPBar1.Minimum = 0
                ProgForm2.CPBar1.Maximum = Convert.ToInt32(Bytes)

                Dim result As DialogResult = MessageBox.Show("Selected  " & TotalFiles & " files have " & CalculateSize.ToString & "" & SizeType, "in total Size", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)

                If result = DialogResult.OK And TextBox1.Text IsNot "" = True Then

                    myProgress = New Progress(Of Integer)(AddressOf ReportProgress)
                         Foldername=Textbox1.Text
                        For Each file In AllowedFiles
                            Try
                                ProgForm2.Show()
                                Await AddFileToRootFolderAsync(file, bucketName, Foldername, myProgress)
                                TheSize = Long.Parse(My.Computer.FileSystem.GetFileInfo(file).Length)

                            Catch ex As Exception
                                MessageBox.Show(ex.Message)
                            End Try

                            ProgForm2.CPBar1.Text = ProgForm2.CPBar1.Value.ToString + "/" + Form2.CPBar1.Maximum.ToString
                        Next

                        ProgForm2.CPBar1.Value = ProgForm2.CPBar1.Maximum
                        If ProgForm2.CPBar1.Value = ProgForm2.CPBar1.Maximum Then
                            ProgForm2.CPBar1.Text = "Task Completed"
                            ProgForm2.Button1.Show()
                        End If
                 Else
                     Exit Sub
                 End if

End Sub

'file uploading function

Public Async Function AddFileToFolderAsync(FileName As String, bucketName As String, folderName As String, ByVal myProgress As IProgress(Of Integer)) As Task


        Try
                If AmazonS3Util.DoesS3BucketExistV2(s3client, bucketName) Then

                    Dim Checkresult = FolderCheck(bucketName, folderName) /'Folder Exist or Not
                    If Checkresult = True Then
                        Dim keyname As String = "" 'destination path(s3 bucket folder)
                        Dim filepath As String = FileName 'current file's local fullpath
                        Dim fname As String = Path.GetFileName(FileName) 'filename
                        If Not folderName.EndsWith("/") Then
                            keyname += folderName & "/"
                            keyname += fname  'bucket's target folder /fname (eg:folder/subfolder/file.mp4)
                        Else
                            keyname += fname  'bucket's target folder /fname (eg:folder/subfolder/file.mp4)
                        End If

                        Dim fileTransferUtility = New TransferUtility(s3client)
                        Dim fileTransferUtilityRequest = New TransferUtilityUploadRequest With {
                            .BucketName = bucketName,
                            .FilePath = filepath,
                            .StorageClass = S3StorageClass.Standard,
                            .ServerSideEncryptionMethod = ServerSideEncryptionMethod.None,
                            .PartSize = 6291456,
                            .Key = keyname,
                            .ContentType = "*.*"}
                        AddHandler fileTransferUtilityRequest.UploadProgressEvent,
                         Sub(sender As Object, e As UploadProgressArgs)
                             Dim percent As Integer = Convert.ToInt32(e.TransferredBytes)  //e.TransferredBytes as long
                             myProgress.Report(percent)
                         End Sub
                        Await fileTransferUtility.UploadAsync(fileTransferUtilityRequest)
                    Else
                        MessageBox.Show(folderName + " folder does not exist")
                    End If
                Else
                    MessageBox.Show(bucketName + " Bucket does not exist")
                End If

            Catch ex As AmazonS3Exception
            MessageBox.Show(ex.Message + " Upload task canceled.")

        Catch ex As Exception
            MessageBox.Show(ex.Message + " Upload task canceled.")

        End Try

    End Function

我陷入了这个困境,不知道我错过了什么。我想对传输到progressbar中目标文件夹的字节进行处理。例如,文件大小为1gb(1073741824字节),那么我如何设置progressbar最大值=1073741824和progressbar值+=transferredbytes。

当您要计算最大值时,您已经通过Select Case机制考虑了TotalSize的值,因此根据其范围对其进行了缩放。但是在ReportProgress中,myInt输入整数直接添加到progressbar值中。我认为您选择的案例也应该在报告进度中实施

编辑1:

让我们假设
TotalSize=109951162777
。对吗?因此,
Byte=1
,progressbar的最大值等于1(即1 TB)。然后,在
ReportProgress
函数中,必须首先将myInt(以字节为单位)除以109951162777,使其成为TB值(例如,0.5 TB),然后更新progressbar的值。如果不这样做,myInt将超过int32限制,并发生错误。我说得对吗? 因此,您必须知道在
Select case
语句中选择了哪个case,TotalSize除以哪个数字?我建议将
按钮1\u单击
功能修改为:

Dim divider as long
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button2.Click

 Dim AllowedFiles As List(Of String) = New List(Of String)

            Try
                Dim TheSize As Long
                Dim TotalSize As Long
                For Each file In AllowedFiles
                    TheSize = Long.Parse((My.Computer.FileSystem.GetFileInfo(file).Length))
                    TotalSize += TheSize
                Next

                Select Case TotalSize
                    Case Is >= 1099511627776
       divider = 1099511627776 'TB                        
                    Case 1073741824 To 1099511627775
                        divider = 1073741824 'GB
                    Case 1048576 To 1073741823
                        divider = 1048576 'MB
                    Case 1024 To 1048575
                        divider = 1024 'KB
                    Case 0 To 1023
                        divider = 1  ' bytes
                    Case Else
                        Bytes = 0
       divider = 1
                        'Return ""
                End Select

                Bytes = CDbl(TotalSize / divider)  'always between 0 and 1

                ProgForm2.CPBar1.Value = 0
                ProgForm2.CPBar1.Minimum = 0
                ProgForm2.CPBar1.Maximum = Convert.ToInt32(Bytes)

                Dim result As DialogResult = MessageBox.Show("Selected  " & TotalFiles & " files have " & CalculateSize.ToString & "" & SizeType, "in total Size", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)

                If result = DialogResult.OK And TextBox1.Text IsNot "" = True Then

                    myProgress = New Progress(Of Integer)(AddressOf ReportProgress)
                         Foldername=Textbox1.Text
                        For Each file In AllowedFiles
                            Try
                                ProgForm2.Show()
                                Await AddFileToRootFolderAsync(file, bucketName, Foldername, myProgress)
                                TheSize = Long.Parse(My.Computer.FileSystem.GetFileInfo(file).Length)

                            Catch ex As Exception
                                MessageBox.Show(ex.Message)
                            End Try

                            ProgForm2.CPBar1.Text = ProgForm2.CPBar1.Value.ToString + "/" + Form2.CPBar1.Maximum.ToString
                        Next

                        ProgForm2.CPBar1.Value = ProgForm2.CPBar1.Maximum
                        If ProgForm2.CPBar1.Value = ProgForm2.CPBar1.Maximum Then
                            ProgForm2.CPBar1.Text = "Task Completed"
                            ProgForm2.Button1.Show()
                        End If
                 Else
                     Exit Sub
                 End if

End Sub
以及
报告进度
,如下所示:

    Form2.CPBar1.Value += myInt / divider ‘to normalize myInt to [0,1]

    Form2.CPBar1.Text = Form2.CPBar1.Value.ToString + "/" + Form2.CPBar1.Maximum.ToString 


希望这能解决问题。

您在哪里获得异常?进入
公共子报告进度(ByVal myInt作为整数)
Form2.CPBar1.Value+=myInt
如何以字节为单位进行TB和GB的进度。我不能这样做。因为1024GB(109951162777字节)。字节值比int32格式太大。我们只能将int32值设置为进度值。我们想将总字节数设置为progressbar的最大值。那么,如何转换它呢。我尝试了
convert.toint32()
integer.parse()
方法。但是,它抛出了一个异常“值太大或太小”,您完全正确。我们必须使用int32值。然而,为了澄清问题并提出解决方案,我在答案中添加了“编辑1”。请阅读,如果问题没有解决,我们将继续。它向我显示了一个参数超出范围异常。我尝试使用2.88mb文件。因此,progressbar的最大值为3。但是,
myint
的值是36。!:{。我已经添加了我对该代码所做的操作。
    Form2.CPBar1.Value += myInt / divider ‘to normalize myInt to [0,1]

    Form2.CPBar1.Text = Form2.CPBar1.Value.ToString + "/" + Form2.CPBar1.Maximum.ToString