2

I have gotten this Upload file via POST -example to work with .txt file. However I am still lacking a way to do the same with a binary file.

Could someone please show how to do that? I have gone over ~100 pages all over the internet of people asking this VERY SAME question... Many of those questions are on this site, but none have an answer that is any good. Codeproject has some ways but they seem to be based on MFC/ATL and I cant use those.

This question seems insanely difficult. :D So please, somebody, answer, pretty please. You would be doing me and quite many other people a favour. :)

Community
  • 1
  • 1
user1412386
  • 31
  • 1
  • 3
  • 1
    Uploading binary is difficult from a browser… from C you just put the binary data after two newlines following the HTTP headers. The example you linked to has the `Content-Type` set to `image/jpeg` which is a binary format… what isn't working or you? Also, the `multipart/form-data` may be unnecessarily complicating things. What sort of service are you uploading to? – Potatoswatter Jul 06 '12 at 19:22
  • Do you control the server part? If not, what's the expected MIME type of the data - form or multipart? And what's so special about binary files as opposed to text ones - you're just not sure how to encode non-ASCII bytes? – Seva Alekseyev Jul 06 '12 at 19:24
  • Uploading through basic html form, like this http://www.tizag.com/phpT/fileupload.php/ on a php platform. When I try uploading image, the with that script, the image goes through but the size of the image is only 4 bytes(iirc), so i think my problem is that i can't handle encoding non-ASCII bytes? :D – user1412386 Jul 06 '12 at 20:42
  • 1
    Possible duplicate of [Is there any good example of http upload using WinInet c++ library](http://stackoverflow.com/questions/471198/is-there-any-good-example-of-http-upload-using-wininet-c-library) – J. Chomel Jul 12 '16 at 15:47
  • Looks like guys sent raw data [with this example](http://stackoverflow.com/a/487990/6019417) – J. Chomel Jul 12 '16 at 15:48

1 Answers1

0

The below code will upload the file: 1.jpg to the specified host using wininet method and a simple http POST request

(name=johndoe and document=1.jpg)

#include<windows.h>
#include<wininet.h>
#include<iostream>

using namespace std;

DWORD rSize,filesize,bytesread,contentlen;

char tmp[2048]; //2Kb Response buffer
char szheaders[1024]; //1kb Header buffer
string chunks; //Server Response

HANDLE hfile;
HINTERNET hOpen2,hConnect2,hReq2;

char szstart[] = "Content-Type: multipart/form-data; boundary=-------------------------acebdf13572468\r\nUser-Agent: Mozilla";
char szData[]    = "---------------------------acebdf13572468\r\nContent-Disposition: form-data; name=\"firstname\"\r\n\r\njohndoe\r\n---------------------------acebdf13572468\r\nContent-Disposition: form-data; name=\"document\"; filename=\"1.jpg\"\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\n\r\n";
char szDataEnd[] = "\r\n---------------------------acebdf13572468--\r\n";

int main()
{

    hfile=CreateFile("1.jpg",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);

    if(hfile != INVALID_HANDLE_VALUE){

    wsprintf(szheaders,szData,"filetoupload","1.jpg");
    hOpen2=InternetOpen(TEXT("Mozilla/5.0"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
    if(hOpen2){
        hConnect2=InternetConnect(hOpen2, TEXT("www.example.com"), INTERNET_DEFAULT_HTTPS_PORT, TEXT(""), TEXT(""), INTERNET_SERVICE_HTTP, 0, 0);
        if(hConnect2){

           hReq2=HttpOpenRequest(hConnect2, TEXT("POST"), TEXT("/your_path/index.php"), TEXT("HTTP/1.1"),NULL,NULL, INTERNET_FLAG_SECURE | INTERNET_FLAG_RELOAD | INTERNET_FLAG_IGNORE_CERT_CN_INVALID |INTERNET_FLAG_IGNORE_CERT_DATE_INVALID, 0);
           if(hReq2){

                filesize=GetFileSize(hfile,NULL);
                contentlen=lstrlen(szheaders)+filesize+lstrlen(szDataEnd);

                LPBYTE pbuf=(LPBYTE)malloc(contentlen);
                CopyMemory(&pbuf[0],szheaders,lstrlen(szheaders));

                ReadFile(hfile,&pbuf[lstrlen(szheaders)],filesize,&bytesread,NULL);
                CopyMemory(&pbuf[lstrlen(szheaders)+filesize],szDataEnd,lstrlen(szDataEnd));

                BOOL status =  HttpSendRequest(hReq2,szstart,lstrlen(szstart),pbuf,contentlen);
                if (status){
                    chunks="";
                    while(InternetReadFile(hReq2, tmp, 2048, &rSize) && rSize > 0)
                    {
                        tmp[rSize] = '\0';
                        chunks += tmp;
                    }
                    cout << chunks;
                    //Prints Server response
                }
                    CloseHandle(hfile);
                    free(pbuf);
            } 
            InternetCloseHandle(hReq2);
        }
        InternetCloseHandle(hConnect2);
    }
    InternetCloseHandle(hOpen2);
    }

}
 

g++ test.cpp -o test.exe -lwininet

A5H1Q
  • 544
  • 7
  • 15