Solved

Determining the length of a file with WinInet

Posted on 2002-04-24
7
663 Views
Last Modified: 2010-04-04
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
Comment
Question by:Richard2000
7 Comments
 
LVL 1

Expert Comment

by:Alone
ID: 6965658
But server does support downloading resume?

WBR...
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 6965790

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
 
LVL 26

Accepted Solution

by:
Russell Libby earned 50 total points
ID: 6965914

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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 12

Expert Comment

by:Lee_Nover
ID: 6967317
monitoring..
haha .. didn't use 'listening' :)
0
 
LVL 45

Expert Comment

by:aikimark
ID: 6967447
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
 
LVL 9

Expert Comment

by:ginsonic
ID: 6967746
interested ;P
0
 

Author Comment

by:Richard2000
ID: 6968148
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question