通过 POST 上传文件

发布于 2024-10-22 04:00:39 字数 3197 浏览 4 评论 0原文

我需要在 C++ 中的 Web 服务器上实现文件上传,我使用以下代码成功:

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


  #define ERROR_OPEN_FILE       10
  #define ERROR_MEMORY          11
  #define ERROR_SIZE            12
  #define ERROR_INTERNET_OPEN   13
  #define ERROR_INTERNET_CONN   14
  #define ERROR_INTERNET_REQ    15
  #define ERROR_INTERNET_SEND   16

  using namespace std;

  int main()
  {
     // Local variables
     static char *filename   = "test.txt";   //Filename to be loaded
     static char *type       = "image/jpg";
     static char boundary[]  = "pippo";            //Header boundary
     static char nameForm[]  = "uploadedfile";     //Input form name
     static char iaddr[]     = "localhost";        //IP address
     static char url[]       = "test.php";         //URL

     char hdrs[255];                  //Headers
     char * buffer;                   //Buffer containing file + headers
     char * content;                  //Buffer containing file
     FILE * pFile;                    //File pointer
     long lSize;                      //File size
     size_t result;                   


     // Open file
     pFile = fopen ( filename , "rb" );
     if (pFile==NULL) return ERROR_OPEN_FILE;

     // obtain file size:
     fseek (pFile , 0 , SEEK_END);
     lSize = ftell (pFile);
     rewind (pFile);

     // allocate memory to contain the whole file:
     content = (char*) malloc (sizeof(char)*lSize);
     if (content == NULL) return ERROR_MEMORY;

     // copy the file into the buffer:
     result = fread (content,1,lSize,pFile);
     if (result != lSize) return ERROR_SIZE;

     // terminate
     fclose (pFile);

     //allocate memory to contain the whole file + HEADER
     buffer = (char*) malloc (sizeof(char)*lSize + 2048);

     //print header
     sprintf(hdrs,"Content-Type: multipart/form-data; boundary=%s",boundary);
     sprintf(buffer,"--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n",boundary,nameForm,filename);
     sprintf(buffer,"%sContent-Type: %s\r\n\r\n",buffer,type);
     sprintf(buffer,"%s%s\r\n",buffer,content);
     sprintf(buffer,"%s--%s--\r\n",buffer,boundary);

     //Open internet connection
     HINTERNET hSession = InternetOpen("WinSock",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
     if(hSession==NULL) return ERROR_INTERNET_OPEN;

     HINTERNET hConnect = InternetConnect(hSession, iaddr,INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
     if(hConnect==NULL) return ERROR_INTERNET_CONN;

     HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",url, NULL, NULL, (const char**)"*/*\0", 0, 1);
     if(hRequest==NULL) return ERROR_INTERNET_REQ;

     BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), buffer, strlen(buffer));
     if(!sent) return ERROR_INTERNET_SEND;

     //close any valid internet-handles
     InternetCloseHandle(hSession);
     InternetCloseHandle(hConnect);
     InternetCloseHandle(hRequest);

     return 0;
  }

从服务器端有一个 PHP 脚本,它使用 $_FILES 变量中存储的信息执行文件保存。 该代码适用于 TXT 文件,但不适用于 JPG(图像)文件:在这种情况下,文件已正确创建并命名为服务器端,但它是空的!我认为这与我读取二进制文件并将其写入字符串有关。 有什么建议吗?

谢谢

I have the necessity to implements a file upload on a web server in C++, I succeed with the following code:

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


  #define ERROR_OPEN_FILE       10
  #define ERROR_MEMORY          11
  #define ERROR_SIZE            12
  #define ERROR_INTERNET_OPEN   13
  #define ERROR_INTERNET_CONN   14
  #define ERROR_INTERNET_REQ    15
  #define ERROR_INTERNET_SEND   16

  using namespace std;

  int main()
  {
     // Local variables
     static char *filename   = "test.txt";   //Filename to be loaded
     static char *type       = "image/jpg";
     static char boundary[]  = "pippo";            //Header boundary
     static char nameForm[]  = "uploadedfile";     //Input form name
     static char iaddr[]     = "localhost";        //IP address
     static char url[]       = "test.php";         //URL

     char hdrs[255];                  //Headers
     char * buffer;                   //Buffer containing file + headers
     char * content;                  //Buffer containing file
     FILE * pFile;                    //File pointer
     long lSize;                      //File size
     size_t result;                   


     // Open file
     pFile = fopen ( filename , "rb" );
     if (pFile==NULL) return ERROR_OPEN_FILE;

     // obtain file size:
     fseek (pFile , 0 , SEEK_END);
     lSize = ftell (pFile);
     rewind (pFile);

     // allocate memory to contain the whole file:
     content = (char*) malloc (sizeof(char)*lSize);
     if (content == NULL) return ERROR_MEMORY;

     // copy the file into the buffer:
     result = fread (content,1,lSize,pFile);
     if (result != lSize) return ERROR_SIZE;

     // terminate
     fclose (pFile);

     //allocate memory to contain the whole file + HEADER
     buffer = (char*) malloc (sizeof(char)*lSize + 2048);

     //print header
     sprintf(hdrs,"Content-Type: multipart/form-data; boundary=%s",boundary);
     sprintf(buffer,"--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n",boundary,nameForm,filename);
     sprintf(buffer,"%sContent-Type: %s\r\n\r\n",buffer,type);
     sprintf(buffer,"%s%s\r\n",buffer,content);
     sprintf(buffer,"%s--%s--\r\n",buffer,boundary);

     //Open internet connection
     HINTERNET hSession = InternetOpen("WinSock",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
     if(hSession==NULL) return ERROR_INTERNET_OPEN;

     HINTERNET hConnect = InternetConnect(hSession, iaddr,INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
     if(hConnect==NULL) return ERROR_INTERNET_CONN;

     HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",url, NULL, NULL, (const char**)"*/*\0", 0, 1);
     if(hRequest==NULL) return ERROR_INTERNET_REQ;

     BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), buffer, strlen(buffer));
     if(!sent) return ERROR_INTERNET_SEND;

     //close any valid internet-handles
     InternetCloseHandle(hSession);
     InternetCloseHandle(hConnect);
     InternetCloseHandle(hRequest);

     return 0;
  }

From server side there is a PHP script that perform file saving using informations stored in $_FILES variable.
The code works well with TXT file, but does not work with JPG (image) files: in this case the file is correctly created and named server side, but it is empty! I think it is related to the fact that I read a binary file and wrote it on a string.
Any suggestion?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风铃鹿 2024-10-29 04:00:39

您不应该对二进制数据使用 sprintf(buffer,"%s%s\r\n",buffer,content); 。它不是字符串,因此 %s 是错误的。

您需要使用适当的 Content-Transfer-Encoding 对其进行 base64 编码,或者使用带有 Content-Length 标头的 memcpy。

You shouldn't be using sprintf(buffer,"%s%s\r\n",buffer,content); on binary data. It isn't a string, so %s is wrong.

You either need to base64 encode it with an appropriate Content-Transfer-Encoding, or use memcpy with a Content-Length header.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文