none
Does anyone know how to upload files to a web server through HTTP by VC++?

Răspunsuri

  • Hi,

     

    You can do this through the MFC WinInet classes. To upload files to a HTTP server, you should firstly create a http connection to the server, then send post request through CHttpConnection:: OpenRequest method, and then use write method of CHttpFile to upload file.

     

    For detail information and sample code, you can refer to

    http://msdn2.microsoft.com/en-us/library/b9zh7b56(VS.71).aspx

    http://www.codeproject.com/KB/IP/http_calls.aspx

     

    Hope this helps!

     

    31 ianuarie 2008 07:03
  •  

    //  m_strFileToTransfer1 is Image file m_strFileToTransfer vedio file which I want to send on Server using Http Connection

    // POST method I do not how to add a vedio filr with "vedio=vediofiledata&image=imagefiledata"  

    //if I send a string with this vedio="prabhat"&image="anish" then it send to server sucessfully

    // last few days I try lot of thing WinInet function but i am not getting full solution pls help me for solve this problem it's bit

    //urgent

     

    BOOL bRead, bRet; 
    DWORD dwBytesRead;
     DWORD dwBytesRead1;
       DWORD dwBytesWritten;
      BOOL result = FALSE;
      DWORD BufferSize = TOTALBYTES;
      LPBYTE pBuffer[8196];

       LPBYTE pBuffer1[8196];
      DWORD dwLength = BufferSize;
      LPSTR Prabhat = "prabhatSingh";
     
      int retVal = 0; //success
         char* url = "http://localhost/homepage/videosend?";
         char* machineList = "";

     /*
     
      HANDLE hFilevedio = CreateFile (m_strFileToTransfer1, GENERIC_READ, FILE_SHARE_READ,NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        
      if (hFilevedio == INVALID_HANDLE_VALUE)
         {
           printf("\nFailed to open local file %s.", m_strFileToTransfer1);
           return FALSE;
         }

      HANDLE hFileimage = CreateFile (m_strFileToTransfer,GENERIC_READ, FILE_SHARE_READ,NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        
      if (hFileimage == INVALID_HANDLE_VALUE)
         {
           printf("\nFailed to open local file %s.", m_strFileToTransfer);
           return FALSE;
         }


    //  HFILE hFilevedio;
    //  OFSTRUCT  fileInformation;
    //  hFilevedio = OpenFile(m_strFileToTransfer1,&fileInformation,OF_READ);
    // DWORD vediosize = GetFileSize(hFilevedio,NULL);
     

     DWORD sum =0; 
     do
      {
        if  (!(bRead = ReadFile (hFilevedio, pBuffer, sizeof(pBuffer),&dwBytesRead, NULL)))
        {
        printf ("\nReadFile failed on buffer %lu.",GetLastError());
       break;
        }

         sum += dwBytesWritten;
      }while (dwBytesRead == sizeof(pBuffer)) ;

      DWORD sum1 =0; 
     do
      {
        if  (!(bRead = ReadFile (hFileimage, pBuffer1, sizeof(pBuffer1),&dwBytesRead1, NULL)))
        {
        printf ("\nReadFile failed on buffer %lu.",GetLastError());
       break;
        }

         sum += dwBytesWritten;
      }while (dwBytesRead1 == sizeof(pBuffer1)) ;

    */


      CFile *f = new CFile();
      CFileStatus status;
      bRet = f->Open( m_strFileToTransfer, CFile::modeRead | CFile::typeBinary);
      f->GetStatus(status);
      long fileSize = status.m_size;
      char *buffer = (char *)malloc(fileSize);
      f->Read(buffer, fileSize);
      f->Close();
      delete f;
      
      CBase64Utils bu;
      char *result2 = bu.Encode(buffer, fileSize);
      int iEncodeLength = strlen(result2);

      // Convert in the byte for sendrequest data
      DWORD dwTlen = (DWORD)(strlen(result2)*sizeof(char)); 


      CFile *fVedio = new CFile();
      CFileStatus status1;
      bool bRetvedio = fVedio->Open( m_strFileToTransfer1, CFile::modeRead | CFile::typeBinary);
      f->GetStatus(status1);
      long fileSize1 = status.m_size;
      char *buffer1 = (char *)malloc(fileSize);
      fVedio->Read(buffer1, fileSize1);
      fVedio->Close();
      delete fVedio;

      CBase64Utils bu1;
      char *result1 = bu1.Encode(buffer1, fileSize1);
      int iEncodeLength1 = strlen(result1);
      
       int urlLength = (int)strlen(url);
       LPWSTR _url = new wchar_t[urlLength + 1];
       if (urlLength + 1 != MultiByteToWideChar(CP_ACP, 0, url,urlLength + 1, _url, urlLength + 1))
       {
          retVal = GetLastError();
         if(_url)
         {
         delete[] _url;
         _url = NULL;
         }
          return retVal;
       }

       _url[urlLength] = 0;


      URL_COMPONENTS urlComp;

      wchar_t hostName[MAX_PATH];
      ZeroMemory(&urlComp, sizeof(urlComp));
      urlComp.dwStructSize = sizeof(urlComp);
      urlComp.lpszHostName = hostName;
      urlComp.dwHostNameLength = MAX_PATH;
      urlComp.dwUrlPathLength = 0xffffffff;


      if (!WinHttpCrackUrl(_url, 0, 0, &urlComp))
       {
        retVal = GetLastError();
        return retVal;
       }
      hostName[urlComp.dwHostNameLength] = 0;


      HINTERNET hSession = NULL;
      HINTERNET hConnect = NULL;
      HINTERNET hRequest = NULL;

      do
      {
                hSession = WinHttpOpen(L"Vedio Image Sender", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,NULL, NULL, 0);
       if (NULL == hSession)
       {
                     retVal = GetLastError();
                     break;
                }
                hConnect = WinHttpConnect(hSession, hostName, urlComp.nPort, 0);
                if (NULL == hConnect)
                {
         retVal = GetLastError();
                        break;
                }


               DWORD dwOpenRequestFlag= (INTERNET_SCHEME_HTTPS == urlComp.nScheme) ? WINHTTP_FLAG_SECURE : 0;


               hRequest = WinHttpOpenRequest(hConnect, L"POST",urlComp.lpszUrlPath, NULL,
                                                                                WINHTTP_NO_REFERER,
                                                                                WINHTTP_DEFAULT_ACCEPT_TYPES,
                                                                                dwOpenRequestFlag);//0);
                if (NULL == hRequest)
                {
                        retVal = GetLastError();
                        break;
                }
       
       // Add a request header.
     //  if( hRequest )
      // bool  bResults = WinHttpAddRequestHeaders( hRequest, L"vedio=\r\n",-1,WINHTTP_ADDREQ_FLAG_ADD );

      
    //   LPVOID  Senddata;
      // Senddata = "vedio=";
         

                if(!WinHttpSendRequest(hRequest,L"Content-Type: application/x-www-form-urlencoded"/*WINHTTP_NO_ADDITIONAL_HEADERS*/,0,WINHTTP_NO_REQUEST_DATA,0, dwTlen, 0))
                {
                        retVal = GetLastError();
                        break;
                }

       result = WinHttpWriteData( hRequest,(LPVOID) result2 , dwTlen, &dwBytesWritten);

      /*
         if(!WinHttpSendRequest(hRequest,WINHTTP_NO_ADDITIONAL_HEADERS,0,WINHTTP_NO_REQUEST_DATA,0, strlen(pszdata), 0))
        {
          retVal = GetLastError();
          break;
        }

        result = WinHttpWriteData( hRequest, pszData, strlen(pszData), &dwBytesWritten);

        if(result)
         {
          MessageBox(NULL,"Prabhat","Prabhat",0);

         }
       */


                if (!WinHttpReceiveResponse(hRequest, NULL))
                {
                        retVal = GetLastError();
                        break;
                }
       
                DWORD nrOfBytesAvailable;
                if (!WinHttpQueryDataAvailable(hRequest, &nrOfBytesAvailable))
                {
                        retVal = GetLastError();
                        break;
                }

                LPSTR data = new char[nrOfBytesAvailable + 1];
                DWORD nrOfBytesRead;
                if (!WinHttpReadData(hRequest, data, nrOfBytesAvailable,&nrOfBytesRead))
                {
                    retVal = GetLastError();
                        break;
                }
                if (nrOfBytesRead != nrOfBytesAvailable)
                {
                        retVal = ERROR_BAD_ARGUMENTS;
                        break;
                }
                data[nrOfBytesAvailable] = '\0';
                machineList = data;


        }while(false);


       if (hRequest)
        {
            WinHttpCloseHandle(hRequest);
        }
       if (hConnect)
        {
            WinHttpCloseHandle(hConnect);
        }
        if (hSession)
        {
            WinHttpCloseHandle(hSession);
        }
            if(_url)
            {
                    delete[] _url;
                    _url = NULL;
            }
            return retVal;

     

    31 ianuarie 2008 14:47

Toate mesajele

  • Hi,

     

    You can do this through the MFC WinInet classes. To upload files to a HTTP server, you should firstly create a http connection to the server, then send post request through CHttpConnection:: OpenRequest method, and then use write method of CHttpFile to upload file.

     

    For detail information and sample code, you can refer to

    http://msdn2.microsoft.com/en-us/library/b9zh7b56(VS.71).aspx

    http://www.codeproject.com/KB/IP/http_calls.aspx

     

    Hope this helps!

     

    31 ianuarie 2008 07:03
  •  

    //  m_strFileToTransfer1 is Image file m_strFileToTransfer vedio file which I want to send on Server using Http Connection

    // POST method I do not how to add a vedio filr with "vedio=vediofiledata&image=imagefiledata"  

    //if I send a string with this vedio="prabhat"&image="anish" then it send to server sucessfully

    // last few days I try lot of thing WinInet function but i am not getting full solution pls help me for solve this problem it's bit

    //urgent

     

    BOOL bRead, bRet; 
    DWORD dwBytesRead;
     DWORD dwBytesRead1;
       DWORD dwBytesWritten;
      BOOL result = FALSE;
      DWORD BufferSize = TOTALBYTES;
      LPBYTE pBuffer[8196];

       LPBYTE pBuffer1[8196];
      DWORD dwLength = BufferSize;
      LPSTR Prabhat = "prabhatSingh";
     
      int retVal = 0; //success
         char* url = "http://localhost/homepage/videosend?";
         char* machineList = "";

     /*
     
      HANDLE hFilevedio = CreateFile (m_strFileToTransfer1, GENERIC_READ, FILE_SHARE_READ,NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        
      if (hFilevedio == INVALID_HANDLE_VALUE)
         {
           printf("\nFailed to open local file %s.", m_strFileToTransfer1);
           return FALSE;
         }

      HANDLE hFileimage = CreateFile (m_strFileToTransfer,GENERIC_READ, FILE_SHARE_READ,NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        
      if (hFileimage == INVALID_HANDLE_VALUE)
         {
           printf("\nFailed to open local file %s.", m_strFileToTransfer);
           return FALSE;
         }


    //  HFILE hFilevedio;
    //  OFSTRUCT  fileInformation;
    //  hFilevedio = OpenFile(m_strFileToTransfer1,&fileInformation,OF_READ);
    // DWORD vediosize = GetFileSize(hFilevedio,NULL);
     

     DWORD sum =0; 
     do
      {
        if  (!(bRead = ReadFile (hFilevedio, pBuffer, sizeof(pBuffer),&dwBytesRead, NULL)))
        {
        printf ("\nReadFile failed on buffer %lu.",GetLastError());
       break;
        }

         sum += dwBytesWritten;
      }while (dwBytesRead == sizeof(pBuffer)) ;

      DWORD sum1 =0; 
     do
      {
        if  (!(bRead = ReadFile (hFileimage, pBuffer1, sizeof(pBuffer1),&dwBytesRead1, NULL)))
        {
        printf ("\nReadFile failed on buffer %lu.",GetLastError());
       break;
        }

         sum += dwBytesWritten;
      }while (dwBytesRead1 == sizeof(pBuffer1)) ;

    */


      CFile *f = new CFile();
      CFileStatus status;
      bRet = f->Open( m_strFileToTransfer, CFile::modeRead | CFile::typeBinary);
      f->GetStatus(status);
      long fileSize = status.m_size;
      char *buffer = (char *)malloc(fileSize);
      f->Read(buffer, fileSize);
      f->Close();
      delete f;
      
      CBase64Utils bu;
      char *result2 = bu.Encode(buffer, fileSize);
      int iEncodeLength = strlen(result2);

      // Convert in the byte for sendrequest data
      DWORD dwTlen = (DWORD)(strlen(result2)*sizeof(char)); 


      CFile *fVedio = new CFile();
      CFileStatus status1;
      bool bRetvedio = fVedio->Open( m_strFileToTransfer1, CFile::modeRead | CFile::typeBinary);
      f->GetStatus(status1);
      long fileSize1 = status.m_size;
      char *buffer1 = (char *)malloc(fileSize);
      fVedio->Read(buffer1, fileSize1);
      fVedio->Close();
      delete fVedio;

      CBase64Utils bu1;
      char *result1 = bu1.Encode(buffer1, fileSize1);
      int iEncodeLength1 = strlen(result1);
      
       int urlLength = (int)strlen(url);
       LPWSTR _url = new wchar_t[urlLength + 1];
       if (urlLength + 1 != MultiByteToWideChar(CP_ACP, 0, url,urlLength + 1, _url, urlLength + 1))
       {
          retVal = GetLastError();
         if(_url)
         {
         delete[] _url;
         _url = NULL;
         }
          return retVal;
       }

       _url[urlLength] = 0;


      URL_COMPONENTS urlComp;

      wchar_t hostName[MAX_PATH];
      ZeroMemory(&urlComp, sizeof(urlComp));
      urlComp.dwStructSize = sizeof(urlComp);
      urlComp.lpszHostName = hostName;
      urlComp.dwHostNameLength = MAX_PATH;
      urlComp.dwUrlPathLength = 0xffffffff;


      if (!WinHttpCrackUrl(_url, 0, 0, &urlComp))
       {
        retVal = GetLastError();
        return retVal;
       }
      hostName[urlComp.dwHostNameLength] = 0;


      HINTERNET hSession = NULL;
      HINTERNET hConnect = NULL;
      HINTERNET hRequest = NULL;

      do
      {
                hSession = WinHttpOpen(L"Vedio Image Sender", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,NULL, NULL, 0);
       if (NULL == hSession)
       {
                     retVal = GetLastError();
                     break;
                }
                hConnect = WinHttpConnect(hSession, hostName, urlComp.nPort, 0);
                if (NULL == hConnect)
                {
         retVal = GetLastError();
                        break;
                }


               DWORD dwOpenRequestFlag= (INTERNET_SCHEME_HTTPS == urlComp.nScheme) ? WINHTTP_FLAG_SECURE : 0;


               hRequest = WinHttpOpenRequest(hConnect, L"POST",urlComp.lpszUrlPath, NULL,
                                                                                WINHTTP_NO_REFERER,
                                                                                WINHTTP_DEFAULT_ACCEPT_TYPES,
                                                                                dwOpenRequestFlag);//0);
                if (NULL == hRequest)
                {
                        retVal = GetLastError();
                        break;
                }
       
       // Add a request header.
     //  if( hRequest )
      // bool  bResults = WinHttpAddRequestHeaders( hRequest, L"vedio=\r\n",-1,WINHTTP_ADDREQ_FLAG_ADD );

      
    //   LPVOID  Senddata;
      // Senddata = "vedio=";
         

                if(!WinHttpSendRequest(hRequest,L"Content-Type: application/x-www-form-urlencoded"/*WINHTTP_NO_ADDITIONAL_HEADERS*/,0,WINHTTP_NO_REQUEST_DATA,0, dwTlen, 0))
                {
                        retVal = GetLastError();
                        break;
                }

       result = WinHttpWriteData( hRequest,(LPVOID) result2 , dwTlen, &dwBytesWritten);

      /*
         if(!WinHttpSendRequest(hRequest,WINHTTP_NO_ADDITIONAL_HEADERS,0,WINHTTP_NO_REQUEST_DATA,0, strlen(pszdata), 0))
        {
          retVal = GetLastError();
          break;
        }

        result = WinHttpWriteData( hRequest, pszData, strlen(pszData), &dwBytesWritten);

        if(result)
         {
          MessageBox(NULL,"Prabhat","Prabhat",0);

         }
       */


                if (!WinHttpReceiveResponse(hRequest, NULL))
                {
                        retVal = GetLastError();
                        break;
                }
       
                DWORD nrOfBytesAvailable;
                if (!WinHttpQueryDataAvailable(hRequest, &nrOfBytesAvailable))
                {
                        retVal = GetLastError();
                        break;
                }

                LPSTR data = new char[nrOfBytesAvailable + 1];
                DWORD nrOfBytesRead;
                if (!WinHttpReadData(hRequest, data, nrOfBytesAvailable,&nrOfBytesRead))
                {
                    retVal = GetLastError();
                        break;
                }
                if (nrOfBytesRead != nrOfBytesAvailable)
                {
                        retVal = ERROR_BAD_ARGUMENTS;
                        break;
                }
                data[nrOfBytesAvailable] = '\0';
                machineList = data;


        }while(false);


       if (hRequest)
        {
            WinHttpCloseHandle(hRequest);
        }
       if (hConnect)
        {
            WinHttpCloseHandle(hConnect);
        }
        if (hSession)
        {
            WinHttpCloseHandle(hSession);
        }
            if(_url)
            {
                    delete[] _url;
                    _url = NULL;
            }
            return retVal;

     

    31 ianuarie 2008 14:47