ZIP文件未完全上传从C++到PHP 我有一个C++控制台应用程序,它把一个zip存档上传到一个PHP页面。上传是工作的文本和图像文件,但不工作的zip文件,我需要
我使用了这里给出的代码:ZIP文件未完全上传从C++到PHP 我有一个C++控制台应用程序,它把一个zip存档上传到一个PHP页面。上传是工作的文本和图像文件,但不工作的zip文件,我需要,c++,C++,我使用了这里给出的代码: 我对C++代码进行了修改,最后的代码如下。 #include <windows.h> #include <wininet.h> #include <iostream> #include <tchar.h> #pragma comment(lib,"wininet.lib") #define ERROR_OPEN_FILE 10 #define ERROR_MEMORY 11 #define
我对C++代码进行了修改,最后的代码如下。
#include <windows.h>
#include <wininet.h>
#include <iostream>
#include <tchar.h>
#pragma comment(lib,"wininet.lib")
#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 = "hello.zip"; //Filename to be loaded
static char *filepath = "C:\\Users\\username\\Desktop\\hello.zip"; //Filename to be loaded
static char *type = "application/zip";
static char boundary[] = "--BOUNDARY---"; //Header boundary
static char nameForm[] = "file"; //Input form name
static char iaddr[] = "localhost"; //IP address
static char url[] = "/u.php"; //URL
char hdrs[512]={'-'}; //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 ( filepath , "rb" );
if (pFile==NULL)
{
printf("ERROR_OPEN_FILE");
getchar();
return ERROR_OPEN_FILE;
}
printf("OPEN_FILE\n");
// 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+1));
if (content == NULL)
{
printf("ERROR_MEMORY");
getchar();
return ERROR_OPEN_FILE;
}
printf("MEMORY_ALLOCATED\t \"%d\" \n",&lSize);
// copy the file into the buffer:
result = fread (content,1,lSize,pFile);
if (result != lSize)
{
printf("ERROR_SIZE");
getchar();
return ERROR_OPEN_FILE;
}
printf("SIZE_OK\n");
content[lSize] = '\0';
// terminate
fclose (pFile);
printf("FILE_CLOSE\n");
//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",buffer,type);
sprintf(buffer,"%s\r\n%s",buffer,content);
sprintf(buffer,"%s\r\n--%s--\r\n",buffer,boundary);
printf("%s", buffer);
//Open internet connection
HINTERNET hSession = InternetOpen("WINDOWS",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(hSession==NULL)
{
printf("ERROR_INTERNET_OPEN");
getchar();
return ERROR_OPEN_FILE;
}
printf("INTERNET_OPENED\n");
HINTERNET hConnect = InternetConnect(hSession, iaddr,INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
if(hConnect==NULL)
{
printf("ERROR_INTERNET_CONN");
getchar();
return ERROR_INTERNET_CONN;
}
printf("INTERNET_CONNECTED\n");
HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",_T(url),NULL, NULL, NULL,INTERNET_FLAG_RELOAD, 1);
if(hRequest==NULL)
{
printf("ERROR_INTERNET_REQ");
getchar();
}
printf("INTERNET_REQ_OPEN\n");
BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), buffer, strlen(buffer));
if(!sent)
{
printf("ERROR_INTERNET_SEND");
getchar();
return ERROR_INTERNET_CONN;
}
printf("INTERNET_SEND_OK\n");
InternetCloseHandle(hSession);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
getchar();
return 0;
}
似乎zip文件没有完全读取,因为只显示前5个字符♥♦而不是整个文件。我尝试过增加HDR的大小以容纳整个内容,但没有成功。zip文件大小约为37KB。请帮助我,因为我不太熟悉在C++中的HTTP请求。
编辑:在我遇到一个类似的问题后,它终于起作用了:你的问题与http请求根本没有关系 基本上,您要问的是:为什么我不能将二进制数据作为字符串处理 答案是:因为printf、strlen等字符串函数在遇到第一个空字符时就停止解析,而zip文件等二进制文件确实包含空字符 因此,在发送数据时,不要试图以字符串形式获取数据的长度: BOOL sent=HttpSendRequesthRequest、hdrs、strlenhdrs、buffer、strlenbuffer
而是使用lSize中已有的长度我应该使用什么替代方法?将其打印为十六进制,或者只打印长度。为什么要显示二进制数据?zip文件未完全上载。在上传文件夹中,ZIP只不过是5字节而不是37 KB存档。你确信问题是在C++代码中,而不是在.php?啊,当尝试显示数据时,你也会做出同样的假设:二进制数据不是字符串,所以你不能使用斯特伦。更新我的答案,也包括这一点
OPEN_FILE
MEMORY_ALLOCATED "2348964"
SIZE_OK
FILE_CLOSE
----BOUNDARY--
Content-Disposition: form-data; name="image"; filename="hello.zip"
Content-Type: application/zip
PK♥♦¶
----BOUNDARY--
INTERNET_OPENED
INTERNET_CONNECTED
INTERNET_REQ_OPEN
INTERNET_SEND_OK