Results 1 to 6 of 6

Thread: [RESOLVED] How to get the size of a file if file is located on a sever

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Resolved [RESOLVED] How to get the size of a file if file is located on a sever

    How can I get the size of a file in MB if the file is located on a server?

    If the file is on a computer I would do this:
    Code:
    Dim file As New IO.FileInfo("c:\myfile.rar"))
    label1.text = file.Length
    ' or: label1.text = (file.Length \ 1024) \ 1024 if i want the size in MB (i guess)
    But what about if the file is located on:
    http://www.mywebsite.com/myfile.rar

    Thank you in advance,

    Andrea.

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: How to get the size of a file if file is located on a sever

    Create a httpwebrequest and get a response object.
    It has contentlength property.

    Code:
       Dim wrq As HttpWebRequest = _
       DirectCast(WebRequest.Create("http://www.mywebsite.com/myfile.rar"), HttpWebRequest)
    
       Dim wrp As HttpWebResponse = _
       DirectCast(HttpWReq.GetResponse(), HttpWebResponse)
    
       MsgBox(String.Format("The file length is {0} bytes.", wrp.ContentLength)
    
    
       wrp.Close()
    Note that in order for this to work the web-server has to be properly configured. In other words, it is not guaranteed that any server will fill in the necessary field.

    The necessary classes are found in the System.Net namespace.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Re: How to get the size of a file if file is located on a sever

    Thanks for the answer!
    I thought there was an easier way to do that.
    By the way, I cannot try the code beacause it gives me an error (I do have System.Net declared):
    Code:
            DirectCast(HttpWReq.GetResponse(), HttpWebResponse) 'HttpWReq is not declared
    If i try this:
    Code:
    DirectCast(HttpWebRequest.GetResponse(), HttpWebResponse) 'reference to a non-shared member requires an object reference
    Any idea?

    Thank you,

    Andrea

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to get the size of a file if file is located on a sever

    it should have been wrq.GetResponse()

    the error tells you that you can't access a non-shared member... means you need an instance to access it... that's what this means: requires an object reference

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Re: How to get the size of a file if file is located on a sever

    Wow thanks! it works!
    Can you please tell me how I can convert the bytes in MB (eg. 137MB)?
    I tried to do this but it gives me an error:

    Code:
    MsgBox(String.Format("The file length is {0} MB.", (wrp.ContentLength / 1024)) / 1024)
    Thanks a lot again,

    Andrea

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Re: How to get the size of a file if file is located on a sever

    my bad!


    got it to work now:
    Code:
    MsgBox(String.Format("The file length is {0:0} MB.", ((wrp.ContentLength / 1024) / 1024)))

    Thanks a lot guys! =D

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width