How To Provide Download/Upload Progress Information when Using WinInet

Izjava o odricanju odgovornosti za sadržaj o otpisanom proizvodu u bazi znanja

Ovaj je članak napisan o proizvodu za koji Microsoft više ne pruža podršku. Članak će stoga ostati nepromijenjen i više se neće ažurirati.


Many developers who are using the WinInet functions to download or upload files on the Internet would like to provide a progress bar to indicate how much of the file transfer has completed and how much longer it will take. You can do this with the following mechanisms.

More Information

Using InternetSetStatusCallback to get notifications of the download progress gives you good information on how the request is progressing, including connecting status notifications. However, it does not indicate that a certain percentage of a transfer has completed.

To get the equivalent of percentage complete notifications, you need to determine the size of the transfer and then call InternetReadFile or InternetWriteFile with small buffers. Then you can calculate the percentage of the transfer as the function calls complete.

For instance, suppose you want to download a 1000 byte file. Instead of making one call to InternetReadFile with a 1000 byte buffer, you can make 10 calls to InternetReadFIle with 100 byte buffers. This way as each call to InternetReadFile completes, you know the download is another 10 percent complete.

The following code illustrates this procedure:


void main(int argc, char *argv[])
if (argc != 3)
cout << "Usage: progress <host> <object>" << endl;

HINTERNET hSession = InternetOpen("WinInet Progress Sample",
HINTERNET hConnection = InternetConnect(hSession,
argv[1], // Server
NULL, // Username
NULL, // Password
0, // Synchronous
NULL); // No Context

HINTERNET hRequest = HttpOpenRequest(hConnection,
NULL, // Default HTTP Version
NULL, // No Referer
(const char**)"*/*\0", // Accept
// anything
0, // Flags
NULL); // No Context
NULL, // No extra headers
0, // Header length
NULL, // No Body
0); // Body length

DWORD dwContentLen;
DWORD dwBufLen = sizeof(dwContentLen);
if (HttpQueryInfo(hRequest,
// You have a content length so you can calculate percent complete
char *pData = (char*)GlobalAlloc(GMEM_FIXED, dwContentLen + 1);
DWORD dwReadSize = dwContentLen / 10; // We will read 10% of data
// with each read.

cout << "Download Progress:" << endl;
cout << " 0----------100%" << endl;
cout << " ";

DWORD cReadCount;
DWORD dwBytesRead;
char *pCopyPtr = pData;
for (cReadCount = 0; cReadCount < 10; cReadCount++)
InternetReadFile(hRequest, pCopyPtr, dwReadSize, &dwBytesRead);
cout << "*";
pCopyPtr = pCopyPtr + dwBytesRead;
// extra read to account for integer division round off
dwContentLen - (pCopyPtr - pData),
// Null terminate data
pData[dwContentLen] = 0;

// Display
cout << endl << "Download Complete" << endl;
cout << pData;
DWORD err = GetLastError();
// No content length...impossible to calculate % complete
// Just read until we are done.
char pData[100];
DWORD dwBytesRead = 1;
while (dwBytesRead)
InternetReadFile(hRequest, pData, 99, &dwBytesRead);
pData[dwBytesRead] = 0;
cout << pData;
There are a few things to look out for when using this approach:
  • You must know the data size before you start. The above code attempts to determine the data size by reading the Content-Length HTTP header using the HttpQueryInfo function. Although many HTTP responses include the Content-Length header, it is not required. Unless you have another mechanism for getting the data size, you will not be able to calculate progress if the Content-Length header is not included in the response.
  • If you are attempting to upload or download from an FTP resource, you will not be able to use FtpPutFile or FtpGetFile and expect to determine progress information. You should use FtpOpenFile and then use InternetReadFile and InternetWriteFile as described above.
  • Because providing progress information assumes you must know the data size, you can use FtpGetFileSize to get the size of an FTP resource before you download a file. Be aware that FtpGetFileSize is not always successful in getting the file size because of the variety of ways that FTP servers return directory listing information. For additional information about problems using FTP to get directory listing information, please see the following article in the Microsoft Knowledge Base:
    172712 INFO: Limitations of WinInet FTP Functions

ID članka: 234913 - posljednja izmjena: 15. srp 2004. - verzija: 1

Povratne informacije