HOWTO: Upload files to the Internet Information ServerID: Q184352
|
There are several options available for uploading files to Internet Information Server (IIS) programmatically from client applications. This articles presents some of these options, and describes how to employ HTTP PUT functionality.
PUT /Test/Page.htm
...
hReq = HttpOpenRequest (hConnect, "PUT", "/test/page.htm", ...)
CHAR szContent [] = "Hello, World!"
HttpSendRequest (hReq, NULL, 0, szContent, lstrlen (szContent))
...
BOOL UseHttpSendReqEx(HINTERNET hConnect, TCHAR *upFile)
{
INTERNET_BUFFERS BufferIn = {0};
DWORD dwBytesRead;
DWORD dwBytesWritten;
BYTE pBuffer[1024]; // Read from file in 1K chunks
BOOL bRead, bRet;
BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS );
HINTERNET hRequest = HttpOpenRequest (hConnect, "PUT",
"/test/page.htm", NULL, NULL, NULL, 0, 0);
if (!hRequest)
{
printf("Failed to open request handle: %lu\n", GetLastError ());
return FALSE;
}
HANDLE hFile = CreateFile (upFile, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("\nFailed to open local file %s.", upFile);
return FALSE;
}
BufferIn.dwBufferTotal = GetFileSize (hFile, NULL);
printf ("File size is %d\n", BufferIn.dwBufferTotal );
if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
{
printf( "Error on HttpSendRequestEx %lu\n",GetLastError() );
return FALSE;
}
DWORD sum = 0;
do
{
if (!(bRead = ReadFile (hFile, pBuffer, sizeof(pBuffer),
&dwBytesRead, NULL)))
{
printf ("\nReadFile failed on buffer %lu.",GetLastError());
break;
}
if (!(bRet=InternetWriteFile( hRequest, pBuffer, dwBytesRead,
&dwBytesWritten)))
{
printf ("\nInternetWriteFile failed %lu", GetLastError());
break;
}
sum += dwBytesWritten;
}
while (dwBytesRead == sizeof(pBuffer)) ;
CloseHandle (hFile);
printf ("Actual written bytes: %d\n", sum);
if(!HttpEndRequest(hRequest, NULL, 0, 0))
{
printf( "Error on HttpEndRequest %lu \n", GetLastError());
return FALSE;
}
return TRUE;
}
PUT /page.test
GET /page.test
PUT /page.test
HttpOpenRequest (hConnect,
"DELETE", // HTTP Verb
"/page.test", // Object
...)
For additional information, please see the following article(s) in the
Microsoft Knowledge Base:
Q177188 FILE: Using HttpSendRequestEx for Large POST Requests(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Leon Braginski, Microsoft Corporation
Additional query words: UPLOAD Posting Acceptor PUT
Keywords :
Version : WINDOWS:1.0,4.0,4.01; winnt:4.0
Platform : WINDOWS winnt
Issue type : kbhowto
Last Reviewed: April 26, 1999