July 23rd, 2002, 12:28 PM
HTTP file upload
I am trying to do a file upload via HTTP using the following code:
BOOL bRetVal = TRUE;
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
if (file.Open(strFile, CFile::modeRead, &e))
// check to see if this is a reasonable URL
CString strServerName, strObject;
if (!AfxParseURL("http://www.MySite.com/" + strFile, dwServiceType, strServerName, strObject, nPort) ||
dwServiceType != INTERNET_SERVICE_HTTP)
pServer = session.GetHttpConnection(strServerName);
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_PUT, strObject);
long lLen = 0;
while (lLen < (long)file.GetLength())
pFile->SendRequest(szHeaders, strlen(szHeaders), (LPVOID)Buffer, 1024);
lLen += 1024;
catch (CInternetException* pEx)
// catch errors from WinINet
bRetVal = FALSE;
if (pFile != NULL)
if (pServer != NULL)
bRetVal = FALSE;
szHeaders is defined as:
const TCHAR szHeaders = _T("User-Agent: Reveal Systems Product WebUpdates\r\nAccept: */*\r\nAccept: binary/*\r\n");
It seems that the CHTTPFile as a read only mode set to it, therefore I'm not able to write to it. I have been able to download files with no problem.
Thanks for the help.
July 23rd, 2002, 02:49 PM
I don't see where your code does anything with the file other than open and close it.
use CInternetFile::WriteString to write the file.
BTW: I don't believe you can do this in any case. Web servers don not usually support or allow users to update files on the server.
That is a large part of the advantage of the web and the HTTP protocol.
If you want to change files on a server, use FTP instead of HTTP.
July 23rd, 2002, 03:07 PM
I'm trying to avoid using FTP because of the username and password that must be supplied.
When I attempt to use the Write function, I get an assertion because the m_bReadMode member of the CInternetFile class is set to TRUE. According to the documentation for SendRequest, the third param is defined as "Any optional data to send immediately after the request headers. This is generally used for POST and PUT operations. This can be NULL if there is no optional data to send."
Any other suggestions?
Thanks in advance.
July 23rd, 2002, 03:19 PM
Nope. I really don't think you can make that work, but Good Luck anyway.
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.