2

My question is the same as is asked in this question:

Get http:/…/File Size

But my issue is that the Content-Length property contains the value of the Content-Length header returned with the response, not the exact file size.

I tried to use FileInfo, but it could only take local file name as parameter - not the remote file.

Can somebody tell me how to get remote file size in bytes or kilobytes?

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • Well as @mdb stated in his answer on the question you linked, if content-length is not present or incorrect, the only way to know precisely the file size is to download it. Have you inspected the HTTP response through fiddler or any other http debug tool to see if the Content-Length header is the same value as the one you get in your code ? – Drewman Mar 29 '13 at 09:53
  • Yes when i matched the value. File which I am downloading is of 5.49 MB and content-length is showing me a number 1448. –  Mar 29 '13 at 09:54
  • Can you post a code sample and maybe the HTTP request and response ? You can use your favourite browser's debug tool (hit F12 key and look for a Network tab) or Fiddler (http://www.fiddler2.com/fiddler2/) for that. – Drewman Mar 29 '13 at 09:59
  • HttpWebRequest wrq = (HttpWebRequest)WebRequest.Create(Prg.source_path); HttpWebResponse wrs = null; try { // HttpWebRespose's object creation wrs = (HttpWebResponse)wrq.GetResponse(); // call to download the file run it without authentication long l = wrs.ContentLength; } catch (System.Net.WebException protocolError) {} Here l contains the content-length but some how I want total file size which is 5.49 MB or in KBs. –  Mar 29 '13 at 10:07

2 Answers2

3

Ok I'll try something

First try this code :

//I am assuming Prg.source_path is the url of the file you want
// Something like : http://www.example.com/my_file.zip
HttpWebRequest wrq = (HttpWebRequest)WebRequest.Create(Prg.source_path); 
//You should be getting only the response header
wrq.Method = "HEAD";

using(var wrs = (HttpWebResponse)wrq.GetResponse()){
    //Do something logic here...
    Console.WriteLine("Content Length is : {0}", wrs.ContentLength);
}

Please ensure that the url of the file you are trying to download is the DIRECT url of the file. If you're being redirected to another location, the header of the response will be the size of the redirect response.

If that code still fails to provide you the correct file size, my guess is that you're either being redirected, or the response is indeed flawed and inaccurate (which is unlikly, but possible).

Can you provide the url's of the files you are trying to download or at least the requests and responses you're sending and getting ?

EDIT 02/04/2013: Also check this question : CSS, Javascript and Images have zero length

See the answer from Adrian Grigore, check that "Control Panel -> Windows Features -> World Wide Web Services - > Common HTTP Features -> Static Content" is checked. If not, check it, restart IIS (or the entire machine, you never know) and try again.

Community
  • 1
  • 1
Drewman
  • 947
  • 11
  • 23
  • What type of web server is hosting the image ? Is the image generated ? Is "test" a folder or some controller name ? – Drewman Mar 29 '13 at 11:52
  • Current I am using IIS server. image type could be anyone(not specific). and test a folder where image is saved. –  Mar 31 '13 at 14:21
0

I know this is an old post. Maybe I don't understand the C# code clearly or misunderstand the question. If I have it right he wants to get size in bytes of a remote file? I use this VB.NET code and it works for me:

Imports System.Net
    
Dim theResponse As HttpWebResponse
Dim theRequest As HttpWebRequest
theRequest = WebRequest.Create("https://www.yoururl.co.za/" & YourFilename)
theResponse = theRequest.GetResponse
Dim FileSizeInBytes As Long = theResponse.ContentLength

Hope this helps somebody.

Ray E
  • 134
  • 1
  • 9