|
-
Feb 9th, 2011, 02:15 PM
#1
Thread Starter
Lively Member
[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.
-
Feb 9th, 2011, 02:26 PM
#2
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.
-
Feb 9th, 2011, 02:49 PM
#3
Thread Starter
Lively Member
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
-
Feb 9th, 2011, 03:48 PM
#4
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
-
Feb 9th, 2011, 04:01 PM
#5
Thread Starter
Lively Member
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
-
Feb 9th, 2011, 04:05 PM
#6
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|