c - 用c语言在localhost上传adobe reader文件

标签 c

I am uploading adobe files in local host it is uploading file successfully but it is showing corrupt or damage file after opening file please help me out to sort out this problem i will be thankful to you below is my entire code

int main()
{
WIN32_FIND_DATA fdFile;
HANDLE hFind = NULL;

TCHAR sPath[2048];
char sDir[2048]= "C:\\boot\\config\\";

//Specify a file mask. *.* = We want everything!

sprintf(sPath, "%s\\*.*", sDir);
//////////////////////////////////////////////////////////

if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE)
{
    printf("Path not found: [%s]\n", sDir);
    return false;
}
do
{
   //Find first file will always return "."
    //    and ".." as the first two directories.
    if(strcmp(fdFile.cFileName, ".") != 0
            && strcmp(fdFile.cFileName, "..") != 0)
    {
        sprintf(sPath, "%s\\%s", sDir, fdFile.cFileName);
 if(fdFile.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY)
        {
            printf("Directory: %s\n", sPath);

        }
        else{
            printf("%s\n", sPath);
static char *type = "text/pdf";
static TCHAR hdrs[] = "Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858";
static char boundary[] = "-----------------------------7d82751e2bc0858";            //Header boundary
static char nameForm[] = "uploadedfile";     //Input form name
static char iaddr[] = "localhost";        //IP address
static char url[] = "/xampp/testing/upload.php?folder=aaaa&&foldername=bbbb";

char * buffer;                   //Buffer containing file + headers
char * content;                  //Buffer containing file
FILE * pFile;                    //File pointer
long lSize;                      //File size
size_t result;
char *pos; // used in the loop

// 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);
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);

rewind (pFile);

if (result != lSize)
{
    printf("ERROR_SIZE");
    getchar();
    return ERROR_OPEN_FILE;
}
printf("SIZE_OK\n");

// 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(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, "%sContent-Length: %d\r\n", buffer, lSize);
strcat(buffer, "\r\n");
int len= strlen(buffer);
memcpy(buffer + len, content, lSize);
char *buf_tail= buffer+len+lSize;
strcpy(buf_tail, "\r\n");
buf_tail += 2; 
strcpy(buf_tail, boundary);
buf_tail += strlen(boundary);
strcpy(buf_tail, "--\r\n");

//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), buf_tail, strlen(buf_tail));

if (!sent)
{
    printf("ERROR_INTERNET_SEND");
    getchar();
    return ERROR_INTERNET_CONN;
}
printf("INTERNET_SEND_OK\n");
printf("\r\n%s\r\n",buffer);

//close any valid internet-handles
InternetCloseHandle(hSession);
InternetCloseHandle(hConnect);
InternetCloseHandle(hRequest);
 }
 }
 }
while(FindNextFile(hFind, &fdFile));
FindClose(hFind); //clean things up!
getch();
return 0;
}

最佳答案

请注意,content(pdf 文件的内容)采用特定的 pdf 格式。当你这样做时:

memcpy(buffer + strlen(buffer),content,lSize);
sprintf(buffer, "%s\r\n", buffer);

然后 sprintf 将在第一个 \0(空字符)处停止打印。

你应该这样做:

//print header
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, "%sContent-Length: %d\r\n", buffer, lSize);

strcat(buffer, "\r\n");
int len= strlen(buffer);                // remember length of header
memcpy(buffer + len, content, lSize);   // append contents
char *buf_tail= buffer+len+lSize;       // start of tail
strcpy(buf_tail, "\r\n");
buf_tail += 2;

strcpy(buf_tail, boundary);
buf_tail += strlen(boundary);
strcpy(buf_tail, "--\r\n");

也就是说,您跟踪缓冲区的末尾并在其中添加数据。

关于c - 用c语言在localhost上传adobe reader文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32943456/

相关文章:

c - 非 int 类型的位字段?

c - 这是在堆上优化多维数组的可能方法吗?

java - 在静态初始化程序中使用 native 调用的 JNI RegisterNatives

c - 如何在 C 中以常数为模计算整数幂

c - 将 csv 内容插入结构数组的函数存在问题

c++ - sprintf 未定义行为

c++ - 我们可以在 main 的参数中添加 CV 限定符吗?

C程序: multi-word guessing game conversion error

c++ - 如何将动态多维数组传递给函数?

c - 在c中使用char数组的总线错误