【问题标题】:How to get the file's size using FtpWebResponse如何使用 FtpWebResponse 获取文件的大小
【发布时间】:2014-06-19 06:14:17
【问题描述】:

尝试从FTP下载文件以获取下载进度我计划实现backgroundWorker,以下代码将在UI中显示下载进度、速度、kb下载量 以下是我在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)
  • 以下是我得到的错误
  • 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) 代码可以运行

【问题讨论】:

    标签: vb.net ftp progress-bar backgroundworker


    【解决方案1】:

    找到以下解决方案以获得找到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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-18
      • 1970-01-01
      相关资源
      最近更新 更多