1

I'm currently usign a Net.WebClient to download a file from the Internet. Now, I'd like to do another thing. I can know the flie size only after I started download with the parameter e.TotalBytesToReceive inside this sub

    Private Sub W_DownloadProgressChanged(ByVal sender As Object, ByVal e As _
    Net.DownloadProgressChangedEventArgs) Handles W.DownloadProgressChanged

How can I get the file size parameter of a link without downloading it?

user1714647
  • 664
  • 2
  • 7
  • 20
  • The correct solution is [here](http://stackoverflow.com/questions/122853/get-http-file-size), where a request for the file is made explicitly asking just for the HEAD. – Alexander Jun 26 '13 at 11:22

1 Answers1

2

Use the WebClient ResponseHeaders:

Public Shared Function GetFileSize(url As String) As Long
    Using obj As New WebClient()
        Using s As Stream = obj.OpenRead(url)
            Return Long.Parse(obj.ResponseHeaders("Content-Length").ToString())
        End Using
    End Using
End Function
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82
  • It's slow because, I am pretty confident, that OpenRead completely downloads the file, and then gives you the stream. Can you test it with a large file (I can't from here), just to be sure? – Alexander Jun 26 '13 at 11:19
  • I doubt it downloads the whole file to get its size. The time to get weightness is equal for files of 1,2 MB and files of 400> MBs – user1714647 Jun 27 '13 at 11:47