C ++로 이미지 파일 업로드

Iyyappan S

저는 C ++ 환경을 처음 사용합니다. 이미지 파일을 C ++로 PHP 서버에 업로드하려고합니다. 그러나 전체 파일을 보내지는 않습니다. 업로드 된 이미지 파일 크기는 1KB뿐입니다.

나는 내 것과 같은 게시물을 얻었습니다. 하지만 이미지 바이너리를 base64 문자열로 변환하는 방법을 모르겠습니다. 다른 솔루션 memcpy로 시도했지만 작동하지 않습니다.

POST를 통해 파일 업로드

내 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);
}

산출:

OPEN_FILE
MEMORY_ALLOCATED     "44358" 
SIZE_OK
FILE_CLOSE
INTERNET_OPENED
INTERNET_CONNECTED
INTERNET_REQ_OPEN
INTERNET_SEND_OK


-----------------------------7d82751e2bc0858

Content-Disposition: form-data; name="uploadedfile"; filename="test.jpg"

Content-Type: text/jpeg



ÿØÿà

-----------------------------7d82751e2bc0858--

감사

Jsantander

바이너리 경로를 유지한다면

//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 통신을 구현하는 많은 라이브러리 중 하나를 보는 것입니다 ( libCURL 은 시작하기에 좋은 곳입니다)

몇 가지 추가 참고 사항 :

대신에

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

하다

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

strlen (buffer)는 첫 번째 널 문자에서 멈 춥니 다.

결과를 표준 출력으로 인쇄 할 때 문자열로 처리되고 ... 첫 번째 null로 끝납니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Cypress 파일 업로드 (이미지 / jpeg)

분류에서Dev

이미지 파일 업로드 문제

분류에서Dev

이미지 이름으로 Grails 파일 업로드

분류에서Dev

jQuery 파일 업로드 미리보기 / 이미지 제거

분류에서Dev

formData 및 Axios로 이미지 파일 업로드

분류에서Dev

Redactor가 간헐적으로 이미지 및 파일 업로드

분류에서Dev

Krajee 파일 입력으로 여러 이미지 업로드

분류에서Dev

파일 업로드 할 이미지 경로 가져 오기

분류에서Dev

IOS에서 웹 서버로 이미지 파일 업로드

분류에서Dev

이미지 업로드 (파일 경로)-JSP

분류에서Dev

양식의 업로드 이미지에 파일 이름 지정

분류에서Dev

이미지 업로드 중 업로드 버튼 및 파일 이름 제거

분류에서Dev

파일 업로드 중지

분류에서Dev

C #으로 업로드 후 WinSCP 이동 파일

분류에서Dev

C # MVC 이미지 업로드 by ajax

분류에서Dev

PHP 파일 업로드 이미지가 작동하지 않음

분류에서Dev

AngularJS 파일 업로드, 업로드 된 이미지가 비어 있습니까?

분류에서Dev

PHP 이미지 업로드 및 고유 파일 이름 할당

분류에서Dev

이미지 업로드 및 원본 이미지 파일 이름 변경

분류에서Dev

PHP 파일로 이미지

분류에서Dev

이미지 파일 경로

분류에서Dev

C #에서 업로드 후 이미지 파일을 삭제하는 방법은 무엇입니까?

분류에서Dev

파일 이름으로 okhttp 멀티 파트 이미지 업로드

분류에서Dev

ASP.NET Core에서 파일 이미지 업로드

분류에서Dev

Flutter 웹-Firebase 저장소에 이미지 파일 업로드

분류에서Dev

파일 업로드 이미지 전용 정규식-opencart

분류에서Dev

Angular Project Directory에있는 파일 / 이미지 업로드

분류에서Dev

삽입 discord.py에 파일 업로드 (이미지 아님)

분류에서Dev

서버에 업로드 한 후 빈 이미지 파일

Related 관련 기사

  1. 1

    Cypress 파일 업로드 (이미지 / jpeg)

  2. 2

    이미지 파일 업로드 문제

  3. 3

    이미지 이름으로 Grails 파일 업로드

  4. 4

    jQuery 파일 업로드 미리보기 / 이미지 제거

  5. 5

    formData 및 Axios로 이미지 파일 업로드

  6. 6

    Redactor가 간헐적으로 이미지 및 파일 업로드

  7. 7

    Krajee 파일 입력으로 여러 이미지 업로드

  8. 8

    파일 업로드 할 이미지 경로 가져 오기

  9. 9

    IOS에서 웹 서버로 이미지 파일 업로드

  10. 10

    이미지 업로드 (파일 경로)-JSP

  11. 11

    양식의 업로드 이미지에 파일 이름 지정

  12. 12

    이미지 업로드 중 업로드 버튼 및 파일 이름 제거

  13. 13

    파일 업로드 중지

  14. 14

    C #으로 업로드 후 WinSCP 이동 파일

  15. 15

    C # MVC 이미지 업로드 by ajax

  16. 16

    PHP 파일 업로드 이미지가 작동하지 않음

  17. 17

    AngularJS 파일 업로드, 업로드 된 이미지가 비어 있습니까?

  18. 18

    PHP 이미지 업로드 및 고유 파일 이름 할당

  19. 19

    이미지 업로드 및 원본 이미지 파일 이름 변경

  20. 20

    PHP 파일로 이미지

  21. 21

    이미지 파일 경로

  22. 22

    C #에서 업로드 후 이미지 파일을 삭제하는 방법은 무엇입니까?

  23. 23

    파일 이름으로 okhttp 멀티 파트 이미지 업로드

  24. 24

    ASP.NET Core에서 파일 이미지 업로드

  25. 25

    Flutter 웹-Firebase 저장소에 이미지 파일 업로드

  26. 26

    파일 업로드 이미지 전용 정규식-opencart

  27. 27

    Angular Project Directory에있는 파일 / 이미지 업로드

  28. 28

    삽입 discord.py에 파일 업로드 (이미지 아님)

  29. 29

    서버에 업로드 한 후 빈 이미지 파일

뜨겁다태그

보관