2017-06-21 56 views
0

我有代码为我的HTTP服务器上上传小.txt文件:发送文件到HTTP服务器(C++的Wininet)

#include <wininet.h> 
#define BUF_SIZE 4096 

void __fastcall TForm1::Button1Click(TObject *Sender) 
{ 

TStringList * list = new TStringList(); 
AnsiString Path = ""; 

if(OpenDialog1->Execute()) { 

    Path = "filename=\""+OpenDialog1->FileName+"\""; 
    list->LoadFromFile(OpenDialog1->FileName); 

} 

char data[BUF_SIZE] = ""; 
static char hdrs[] = "Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858"; 

strcat(data,"-----------------------------7d82751e2bc0858"); 
strcat(data,"\n"); 
strcat(data,"Content-Disposition: form-data; name=\"files[]\"; "); 
strcat(data,Path.c_str()); 
strcat(data,"\n"); 
strcat(data,"Content-Type: application/octet-stream"); 
strcat(data,"\n\n"); 
strcat(data,list->Text.c_str()); 
strcat(data,"\n"); 
strcat(data,"-----------------------------7d82751e2bc0858"); 
strcat(data,"\n"); 
strcat(data,"Content-Disposition: form-data; name=\"user\""); 
strcat(data,"\r\n\r\n"); 
strcat(data,"username"); 
strcat(data,"\r\n"); 
strcat(data,"-----------------------------7d82751e2bc0858"); 
strcat(data,"\n"); 

HINTERNET hSession = InternetOpen("MyAgent",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); 
HINTERNET hConnect = InternetConnect(hSession, "localhost",INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1); 
HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", "new_upltest.php", NULL, NULL, NULL, 0, 1); 

HttpSendRequest(hRequest, hdrs, strlen(hdrs), data, strlen(data)); 

delete list; 
} 

它的正常工作,但现在我需要上传大文件(超过70 -100 MB),可以是图像(.jpg,.png,.bmp)和其他文档类型(.pdf,.docx等)。

是否有可能通过此代码解决我的任务?我将不胜感激每一个建议...

P.S.我的IDE是C++ Builder 6,但我认为没关系。

+0

如果您在C++编程,你为什么不使用'的std :: string'的“数据”?或者使用'std :: ostringstream'进行格式化?为什么动态分配'list'而不是将其定义为一个简单的对象实例? –

+1

@Someprogrammerdude,问题中的IDE将回答您的所有问题:) – Incomputable

+0

2002年编译器* facepalm *。那么,它至少比TurboC++ 3更新。 –

回答

1

一个文件上传这是工作例如:

void http_upload_file(PCHAR szServer, PCHAR szScript, PCHAR szParam, PCHAR szValue, PCHAR szFile) 
{ 
    PCHAR szHeaders = "Content-Type: multipart/form-data; boundary=----qwerty"; 
    PCHAR szData = "------qwerty\r\n" 
         "Content-Disposition: form-data; name=\"%s\"\r\n\r\n%s\r\n" 
         "------qwerty\r\n" 
         "Content-Disposition: form-data; name=\"files[]\"; filename=\"%s\"\r\n" 
         "Content-Type: application/octet-stream\r\n" 
         "Content-Transfer-Encoding: binary\r\n\r\n"; 
    PCHAR szDataEnd = "\r\n------qwerty--\r\n"; 
    char szHeader[512]; 

    HINTERNET hSession, hConnect, hRequest; 
    DWORD  dwFileSize, dwBytesRead, dwContentLength,dwBytesWritten; 

    hSession = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); 

    if (hSession) 
    { 
     hConnect = InternetConnect(hSession, szServer, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP,0, 0); 

     if (hConnect) 
     { 
      hRequest = HttpOpenRequest(hConnect, "POST", szScript, NULL, NULL, 0, 0, 0); 

      if (hRequest) 
      { 
       HANDLE hFile = CreateFile(szFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL); 

       if (hFile != INVALID_HANDLE_VALUE) 
       { 
        dwFileSize  = GetFileSize(hFile, NULL); 
        wsprintf(szHeader, szData, szParam, szValue, szFile); 
        dwContentLength = lstrlen(szHeader) + dwFileSize + lstrlen(szDataEnd); 
        LPBYTE pBuf  = (LPBYTE)malloc(dwContentLength); 
        CopyMemory(&pBuf[0], szHeader, lstrlen(szHeader)); 
        ReadFile(hFile, &pBuf[lstrlen(szHeader)], dwFileSize, &dwBytesRead, NULL); 
        CopyMemory(&pBuf[lstrlen(szHeader) + dwFileSize], szDataEnd, lstrlen(szDataEnd)); 
        HttpSendRequest(hRequest, szHeaders, lstrlen(szHeaders), pBuf, dwContentLength); 
        CloseHandle(hFile); 
        free(pBuf); 
       } 
      } 

      InternetCloseHandle(hRequest); 
     } 

     InternetCloseHandle(hConnect); 
    } 

    InternetCloseHandle(hSession); 
}