问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/17316293/request-file-size-before-download-it