• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 675
  • Last Modified:

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
0
Richard2000
Asked:
Richard2000
1 Solution
 
AloneCommented:
But server does support downloading resume?

WBR...
0
 
Russell LibbySoftware Engineer, Advisory Commented:

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
0
 
Russell LibbySoftware Engineer, Advisory Commented:

Richard,

Did some more research into this (MSDN and other sources). In cases where you are not able to get the resource size, there is not much your going to be able to do in regards to displaying upload/download progress. One possible work around to this is to modify your progress to be timed based, as well as progress based. For the resources where you are not able to get size, you could then update the meter based on timed increments, giving the user the appearance of progress. Hope this helps.

Russell


0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Lee_NoverCommented:
monitoring..
haha .. didn't use 'listening' :)
0
 
aikimarkCommented:
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?
0
 
ginsonicCommented:
interested ;P
0
 
Richard2000Author Commented:
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
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now