如何使用FtpWebResponse获取文件的大小

时间:2014-06-19 06:14:18

标签: vb.net ftp progress-bar backgroundworker

尝试从FTP下载文件以获得我计划实施backgroundWorker的下载进度,以下代码将显示下载进度,速度,下载kb的数量UI 以下是我在backgroundWorker_doWork

中编写的代码
 'Creating the request and getting the response
    Dim theResponse As FtpWebResponse
    Dim theRequest As FtpWebRequest
    Try 
        'Checks if the file exist
        theRequest = WebRequest.Create(Me.txtFileName.Text)
        theResponse = theRequest.GetResponse
    Catch ex As Exception
        MessageBox.Show("An error occurred while downloading file. Possible causes:" & ControlChars.CrLf & _
                        "1) File doesn't exist" & ControlChars.CrLf & _
                        "2) Remote server error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
        Me.Invoke(cancelDelegate, True)
        Exit Sub
    End Try

    Dim length As Long = theResponse.ContentLength 'Size of the response (in bytes)
    Dim safedelegate As New ChangeTextsSafe(AddressOf ChangeTexts)
    Me.Invoke(safedelegate, length, 0, 0, 0) 'Invoke the TreadsafeDelegate
    Dim writeStream As New IO.FileStream(Me.whereToSave, IO.FileMode.Create)
    'Replacement for Stream.Position (webResponse stream doesn't support seek)
    Dim nRead As Integer
    'To calculate the download speed
    Dim speedtimer As New Stopwatch
    Dim currentspeed As Double = -1
    Dim readings As Integer = 0

    Do
        If BackgroundWorker1.CancellationPending Then 'If user abort download
            Exit Do
        End If
        speedtimer.Start()
        Dim readBytes(4095) As Byte
        Dim bytesread As Integer = theResponse.GetResponseStream.Read(readBytes, 0, 4096)
        nRead += bytesread
        Dim percent As Short = (nRead * 100) / length
        Me.Invoke(safedelegate, length, nRead, percent, currentspeed)
        If bytesread = 0 Then Exit Do
        writeStream.Write(readBytes, 0, bytesread)
        speedtimer.Stop()
        readings += 1
        If readings >= 5 Then 'For increase precision, the speed it's calculated only every five cicles
            currentspeed = 20480 / (speedtimer.ElapsedMilliseconds / 1000)
            speedtimer.Reset()
            readings = 0
        End If
    Loop

    'Close the streams
    theResponse.GetResponseStream.Close()
    writeStream.Close()

    If Me.BackgroundWorker1.CancellationPending Then
        IO.File.Delete(Me.whereToSave)
        Dim cancelDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
        Me.Invoke(cancelDelegate, True)
        Exit Sub
    End If

    Dim completeDelegate As New DownloadCompleteSafe(AddressOf DownloadComplete)
    Me.Invoke(completeDelegate, False)
  • 以下是我得到的错误 enter image description here
  • Dim length As Long = theResponse.ContentLength获取-1
  • 例如
  

ftp://username:mypassword@ftp.drivehq.com/masters/5/party/party.csv

    上面给出的
  • ftp Url传递到下载文件,这里party.csv是要下载的文件 注意:问题是在我的情况下获取要下载的文件的文件大小party,csvDim length As Long = theResponse.ContentLength。如果我更改,theResponse.ContentLength获取-1文件的实际大小为420KB Dim length As Long =430080(420kb = 430080bytes)代码可以正常工作

1 个答案:

答案 0 :(得分:0)

找到以下解决方案以获取找到file's size

的确切方法
    Dim request As FtpWebRequest = DirectCast(WebRequest.Create(Me.txtFileName.Text), FtpWebRequest)
    request.Method = WebRequestMethods.Ftp.GetFileSize
    Dim response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
    Dim fileSize As Long = response.ContentLength