• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3201
  • Last Modified:

How to use Wininet to upload a file via HTTP and check the response

Dear Experts,
I would like to upload a file using Wininet and it actually seems to work.
I would like to see the response of my PHP script which is supposed to receive the file.
This doesn't work in my code below.

While at it, how can I specify the variable name for the receiving script?

bool CTask::UploadFile(HINTERNET hINet, HINTERNET hCon, const char *filePath) {
     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 (hCon, "PUT",
         "/2DB/admin/upAMS.php", NULL, NULL, NULL,  0, 0);
     if (!hRequest)
       return(false);
     HANDLE hFile = CreateFile (filePath, GENERIC_READ, FILE_SHARE_READ,
         NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
     if (hFile == INVALID_HANDLE_VALUE)
            return(false);
     BufferIn.dwBufferTotal = GetFileSize (hFile, NULL);
     if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
       return(false);
     DWORD sum = 0;
     do
     {
       if  (!(bRead = ReadFile (hFile, pBuffer, sizeof(pBuffer),
           &dwBytesRead, NULL)))
         break;
       if (!(bRet=InternetWriteFile( hRequest, pBuffer, dwBytesRead,
           &dwBytesWritten)))
         break;
       sum += dwBytesWritten;
     }
     while (dwBytesRead == sizeof(pBuffer)) ;

// The below just won't work for me
// What I want is to get the server response after internetwritefile finishes (which seems to work fine).

      CString resp;
      char pcBuffer[500];
      printf("\nThe following was returned by the server:\n");
      do
      {      dwBytesRead=0;
            if(InternetReadFile(hFile, pcBuffer, 499, &dwBytesRead))
            {
                  pcBuffer[dwBytesRead]=0x00; // Null-terminate buffer
                  resp += pcBuffer;
            }
            else
                  printf("\nInternetReadFile failed");
      }while(dwBytesRead>0);

     CloseHandle (hFile);
     if(!HttpEndRequest(hRequest, NULL, 0, 0))
     {
       return(false);
     }
     return(true);
}

I have been trying this for a while and it just won't work.
0
allmer
Asked:
allmer
  • 3
1 Solution
 
Deepu AbrahamR & D Engineering ManagerCommented:
This one will give you both upload and download progress
http://support.microsoft.com/kb/234913
There is a good sample as well.

Best Regards,
DeepuAbrahamK
0
 
allmerAuthor Commented:
I would appreciate if you could point out my mistake.
Thanks
0
 
Deepu AbrahamR & D Engineering ManagerCommented:
Well you need to use somithing like,
InternetStatusCallback() to know the status of your file writing. It will not work in your code since you have called InternetReadFile sequencially.
Best Regards,
DeepuAbrahamK
0
 
Deepu AbrahamR & D Engineering ManagerCommented:
http://msdn2.microsoft.com/en-us/library/aa383931.aspx
You can go through this as well.
Hope this is what you are looking for.
Best Regards,
DeepuAbrahamK
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now