HTTP file upload
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: HTTP file upload

  1. #1
    Join Date
    Jul 2002
    Location
    Denver, CO USA
    Posts
    11

    Angry HTTP file upload

    I am trying to do a file upload via HTTP using the following code:

    BOOL bRetVal = TRUE;

    CInternetSession session;
    CHttpConnection* pServer = NULL;
    CHttpFile* pFile = NULL;

    CFile file;
    CFileException e;
    if (file.Open(strFile, CFile::modeRead, &e))
    {
    try
    {
    // check to see if this is a reasonable URL
    CString strServerName, strObject;

    INTERNET_PORT nPort;
    DWORD dwServiceType;

    if (!AfxParseURL("http://www.MySite.com/" + strFile, dwServiceType, strServerName, strObject, nPort) ||
    dwServiceType != INTERNET_SERVICE_HTTP)
    {
    ASSERT(FALSE);
    return FALSE;
    }

    pServer = session.GetHttpConnection(strServerName);

    pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_PUT, strObject);

    long lLen = 0;
    BYTE Buffer[1024];
    while (lLen < (long)file.GetLength())
    {
    file.Read(Buffer, 1024);
    pFile->SendRequest(szHeaders, strlen(szHeaders), (LPVOID)Buffer, 1024);

    ZeroMemory(Buffer, 1024);
    lLen += 1024;
    }
    pFile->EndRequest();

    if(pFile)
    pFile->Close();

    pServer->Close();
    }
    catch (CInternetException* pEx)
    {
    // catch errors from WinINet
    TCHAR szErr[1024];
    pEx->GetErrorMessage(szErr, 1024);
    pEx->Delete();
    bRetVal = FALSE;
    }

    if (pFile != NULL)
    delete pFile;

    if (pServer != NULL)
    delete pServer;

    session.Close();

    file.Close();
    }
    else
    bRetVal = FALSE;

    return bRetVal;


    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.

  2. #2
    Join Date
    Aug 2001
    Location
    North Bend, WA
    Posts
    1,947
    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.

  3. #3
    Join Date
    Jul 2002
    Location
    Denver, CO USA
    Posts
    11
    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.

  4. #4
    Join Date
    Aug 2001
    Location
    North Bend, WA
    Posts
    1,947
    Nope. I really don't think you can make that work, but Good Luck anyway.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center