Link to home
Start Free TrialLog in
Avatar of Richard2000
Richard2000

asked on

Determining the length of a file with WinInet

Hi,

I am using this code with the asynchronous WinInet to determine the length of a file...

dwBufferLen:=SizeOf(pr.dwTotalLength);
dwIndex:=0;
if (HttpQueryInfo(pr.hOpenURL, HTTP_QUERY_CONTENT_LENGTH or HTTP_QUERY_FLAG_NUMBER, @pr.dwTotalLength, dwBufferLen, dwIndex) = FALSE) then pr.dwTotalLength:=0;

This is done so that I can display a progress bar whilst the file is downloading.  For many sites this works fine, however for some sites I find that it always returns zero.  For example...

www.google.com
www.yahoo.com
www.ask.com

As a result I can't display a progress bar for some sites when the download takes place because I am unable to determine the length of the file before the download starts, so it isn't possible to know how much progress has been made.

I guess it doesn't work with some sites because the length information isn't stored in the HTTP header??  Do you know of any way around this problem?

Thanks in Advance,

Richard
Avatar of Alone
Alone

But server does support downloading resume?

WBR...
Avatar of Russell Libby

Richard,

It is possible that the servers on these sites do not generate the "Content-Length:" header item. This is not a required header, though most sites do return it. If I remember correctly, this is the header used to return the results for the HTTP_QUERY_CONTENT_LENGTH flag. Other than that, I'm not sure why it would fail.
Have you checked GetLastError() in the cases where it does fail? This may help give you/us a little more info to go on.

Russell
ASKER CERTIFIED SOLUTION
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
monitoring..
haha .. didn't use 'listening' :)
Can you inspect the TCP/IP packet information?
When transfering files, the individual packets are numbered and (I think) include the total number of packets being sent.

Will any other transfer protocol, such as FTP, provide size or transfer progress data?
interested ;P
Avatar of Richard2000

ASKER

Hi Russell and all,

Thank you for your comments and for looking into this for me.  I thought that the problem was caused by the length information not being present because it works fine on most sites, but fails on some sites.

So this is what I've chosen to do.  For sites where the length information isn't present, I estimate the length and use that for the progress bar.  Since it is mainly web pages that I am downloading, I use 20K as the length and make that go up to 80% on the progress bar and no further.  Then when the download has finally completed I set this to 100%.  Of course, this isn't 100% accurate but it is better than having a blank progress bar and it is hardly noticable.  It is also easy to code.

Richard