Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C+中上载图像文件+; 我对C++环境很陌生。我尝试在C++上传一个图像文件到PHP服务器。但它不会发送完整的文件。只有我上传的图像文件大小是1KB_C++_File Upload_Http Post - Fatal编程技术网

在C+中上载图像文件+; 我对C++环境很陌生。我尝试在C++上传一个图像文件到PHP服务器。但它不会发送完整的文件。只有我上传的图像文件大小是1KB

在C+中上载图像文件+; 我对C++环境很陌生。我尝试在C++上传一个图像文件到PHP服务器。但它不会发送完整的文件。只有我上传的图像文件大小是1KB,c++,file-upload,http-post,C++,File Upload,Http Post,我得到了一个和我一样的职位。但我不知道如何将图像二进制转换为base64字符串。我尝试了另一个解决方案memcpy,它也不起作用 我的C++代码: #define _CRT_SECURE_NO_DEPRECATE #include <iostream> #include <tchar.h> #include <Urlmon.h> #pragma comment (lib, "Urlmon.lib") #include <windows.h> #

我得到了一个和我一样的职位。但我不知道如何将图像二进制转换为base64字符串。我尝试了另一个解决方案memcpy,它也不起作用

<>我的C++代码:

#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <tchar.h>
#include <Urlmon.h>
#pragma comment (lib, "Urlmon.lib")

#include <windows.h>
#include <wininet.h>
#include <iostream>
#include <tchar.h>
#include <stdio.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 cv;
using namespace std;

int main()
{
    // Local variables
    static char *filename = "test.jpg";   //Filename to be loaded
    static char *filepath = "test.jpg";   //Filename to be loaded
    static char *type = "text/jpeg";
    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[] = "server";        //IP address
    static char url[] = "uploader.php";

    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);
    sprintf(buffer, "%s\r\n%s\r\n", buffer, content);

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

    //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");
    printf("\r\n%s\r\n",buffer);

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

如果您坚持二进制路由,谢谢

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

//print header
chars+=sprintf(buffer+chars, "%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n", boundary, nameForm, filename);
chars+=sprintf(buffer+chars, "Content-Type: %s\r\n", type);
chars+=sprintf(buffer+chars, "Content-Length: %d\r\n", lSize);

chars+=sprintf(buffer+chars, "\r\n");
memcpy(buffer + chars,content,lSize);
chars+=lSize;
chars+=sprintf(buffer+chars, "\r\n");

chars+=sprintf(buffer+chars, "%s--\r\n", boundary);
但是在任何情况下,我的建议是让您看看实现HTTP通信的许多库中的任何一个(这是一个很好的开始)

一些补充说明:

而不是

BOOL sent = HttpSendRequest(hRequest, hdrs, strlen(hdrs), buffer, strlen(buffer));

as strlen(缓冲区)将在第一个空字符处停止


当您将结果打印到std out时,它将被视为字符串。。。。并以第一个null结尾。

有没有理由不使用libcurl或类似的东西?您正在打印内容,就像它是一个字符串一样,这意味着它将在第一个0处停止。此外,您总是在同一个位置快速运行。。。你需要提高缓冲点。是的,我发现上面没有多少lib。。但是我试着运行这个代码。。我最后的选择是第三方图书馆Hi jsantander。。如何打印高级缓冲区指针你能给我一些示例代码吗?嗨,我试过你的代码,但它打印了两次二进制文件-----------------------------7d82751e2bc0858内容处理:表单数据;name=“uploadedfile”;filename=“test.jpg”内容类型:text/jpeg内容长度:44358。。。我被剪切并粘贴了你的版本,取消了memcopy部分的注释。。。但是忘了删除一个sprintf。。。。现在检查?它不会将结束边界打印到缓冲区。图像二进制也有相同的值。它不读取完整文件。@IyyappanS这是因为
printf(“\r\n%s\r\n”,缓冲区)您正在将缓冲区打印为在第一个0处停止的字符串。好的,我理解。。但我在服务器上找不到上传的文件
BOOL sent = HttpSendRequest(hRequest, hdrs, strlen(hdrs), buffer, strlen(buffer));
BOOL sent = HttpSendRequest(hRequest, hdrs, strlen(hdrs), buffer, chars);