user1714647
user1714647

Reputation: 664

Request file size before download it

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?

Upvotes: 1

Views: 2494

Answers (1)

Carlos Landeras
Carlos Landeras

Reputation: 11063

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

Upvotes: 2

Related Questions