Request file size before download it

会有一股神秘感。 提交于 2020-01-24 23:13:11

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!