用C++上传图像文件
Upload Image File in C++
我是C++环境的新手。我尝试用C++将一个图像文件上传到PHP服务器。但它不会发送完整的文件。只有我上传的图像文件大小是1 KB。
我得到了一个非常像我的职位。但我不知道如何将图像二进制转换为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_FILEn");
// 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_ALLOCATEDt "%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_OKn");
// terminate
fclose(pFile);
printf("FILE_CLOSEn");
//allocate memory to contain the whole file + HEADER
buffer = (char*)malloc(sizeof(char)*lSize + 2048);
//print header
sprintf(buffer, "%srnContent-Disposition: form-data; name="%s"; filename="%s"rn", boundary, nameForm, filename);
sprintf(buffer, "%sContent-Type: %srn", buffer, type);
//sprintf(buffer, "%sContent-Length: %drn", buffer, lSize);
sprintf(buffer, "%srn%srn", buffer, content);
/**
sprintf(buffer, "%srn", buffer);
memcpy(buffer + strlen(buffer),content,lSize);
sprintf(buffer, "%srn", buffer);
*/
sprintf(buffer, "%s%s--rn", 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_OPENEDn");
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_CONNECTEDn");
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_OPENn");
BOOL sent = HttpSendRequest(hRequest, hdrs, strlen(hdrs), buffer, strlen(buffer));
if (!sent)
{
printf("ERROR_INTERNET_SEND");
getchar();
return ERROR_INTERNET_CONN;
}
printf("INTERNET_SEND_OKn");
printf("rn%srn",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--
感谢
如果您保持二进制路由
//allocate memory to contain the whole file + HEADER
buffer = (char*)malloc(sizeof(char)*lSize + 2048);
int chars=0;
//print header
chars+=sprintf(buffer+chars, "%srnContent-Disposition: form-data; name="%s"; filename="%s"rn", boundary, nameForm, filename);
chars+=sprintf(buffer+chars, "Content-Type: %srn", type);
chars+=sprintf(buffer+chars, "Content-Length: %drn", lSize);
chars+=sprintf(buffer+chars, "rn");
memcpy(buffer + chars,content,lSize);
chars+=lSize;
chars+=sprintf(buffer+chars, "rn");
chars+=sprintf(buffer+chars, "%s--rn", boundary);
但在任何情况下,我的建议是让您看看现有的许多实现HTTP通信的库中的任何一个(libCURL是一个很好的起点)
一些附加说明:
而不是
BOOL sent = HttpSendRequest(hRequest, hdrs, strlen(hdrs), buffer, strlen(buffer));
进行
BOOL sent = HttpSendRequest(hRequest, hdrs, strlen(hdrs), buffer, chars);
因为strlen(缓冲区)将在第一个空字符处停止。
当您将结果打印到stdout时,它将被视为字符串。。。。并以第一个null结束。
相关文章:
- 在qt窗口中具有图像,而无需将图像文件本身作为源
- 如何从所选目录(位于listWidget中)中筛选图像文件
- 如何将图像文件夹添加到CMake项目以便在c ++可执行文件中使用这些图像?
- 解码并保存 base64 C++中的图像文件
- BASE64图像文件用C 编码
- 如何在使用 c++/COM 发送时在电子邮件中插入/嵌入图像文件(.png)
- 如何使用Imebra库将压缩像素(用我自己的编码器压缩)回到DICOM图像文件中
- 使用 Imread 打开具有 Unicode 名称的图像文件
- 如何使用stb_image将像素颜色数据写入 BMP 图像文件?
- C 读取/写作PPM图像文件灰色图像
- 如何在C 中删除或删除.jpg和.dpx图像文件
- C++图像处理 - 将图像文件读入 2D 数组
- 将图像文件(.BMP)复制到其他位置
- 从位图图像文件读取像素数据值
- Boost:Asio.从服务器下载图像文件
- 打开不同的图像文件,并使用GDI 库在主应用程序窗口的背景上绘制它们
- 使用 QPixmap 和 QLabel 在 Qt Creator 中以编程方式添加图像文件 (.PNG)
- C++:将整数的文本文件转换为BMP格式的位图图像文件
- 读取/写入PPM图像文件C++
- 将C++图像文件转换为数组