1

I am trying to upload a PDF file to my PHP page with my code. But I can only upload, a little of part it. When I open it in Firefox it says the PDF is damaged. Also, when I look at the value of a it's just a small part of file. How I can solve this?

char *getmem();
int main(int argc, char *argv[]) 
{

char *a = getmem();
printf ("%s\n",a);
        HINTERNET hInet = InternetOpen(NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
char frmdata[160000];
sprintf(frmdata,"-----------------------------acebdf13572468\r\nContent-Disposition: form-data; name=\"uploaded\"; filename=\"C:\\Users\\pen\\Desktop\\perltut.pdf\"\r\nContent-Type: application/pdf\r\n\r\n%s\r\n-----------------------------acebdf13572468--\r\n",a);

 char *hdrs = "Content-Type: multipart/form-data; boundary=---------------------------acebdf13572468\nUser-Agent: aa";
MessageBox(0,a,a,1);
        HINTERNET hConnect = InternetConnect(
                        hInet,
                        "host",
                        80,
                        NULL,
                        NULL,
                        INTERNET_SERVICE_HTTP,
                        0, 0
        );
LPCSTR accept[2]={"*/*", NULL};
        HINTERNET hRequest = HttpOpenRequest(
                hConnect,
                "POST",
                "/path",
                NULL, NULL, NULL, 0, 0
        );

        BOOL ret = HttpSendRequest(hRequest, hdrs,strlen(hdrs), frmdata, strlen(frmdata));





    system("pause");
    return 0;
}



char *getmem(){


FILE *fp;
long len;
char *buf;
fp=fopen("C:\\Users\\ff\\Desktop\\perltut.pdf","rb");
fseek(fp,0,SEEK_END); 
len=ftell(fp); 
fseek(fp,0,SEEK_SET); /
buf=(char *)malloc(len); 
fread(buf,len,1,fp); /
fclose(fp);
return buf;
}   
4

1 回答 1

0

您的文件包含零字符,它被解释为字符串的结尾。您不能将 sprintf 用于二进制数据。

于 2013-03-16T10:58:55.647 回答