c++ - 使用cpp-httplib上传文件

标签 c++ http http-post

我正在使用cpp-httplib我正在尝试将文件上传到测试后服务器,例如 https://ptsv2.com 。我不太了解有关文件上传的给定文档,我有点理解您必须使用 Multipart/form-data 才能上传文件。

文档中给定的代码是这样的

httplib::MultipartFormDataItems items = {
  { "text1", "text default", "", "" },
  { "text2", "aωb", "", "" },
  { "file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain" },
  { "file2", "{\n  \"world\", true\n}\n", "world.json", "application/json" },
  { "file3", "", "", "application/octet-stream" },
};

auto res = cli.Post("/multipart", items);

在file1中,您可以看到它正在服务器上创建一个文件并将其命名为hello.txt。 如何编写它以便可以将位于我的设备上的文件上传到服务器上?

任何建议都会有帮助。谢谢。

最佳答案

根据@Botje和cpp-httplib文档

std::ifstream t_lf_img("lfimg.png");
std::stringstream buffer_lf_img;
buffer_lf_img << t_lf_img.rdbuf();

std::ifstream t_pc_file("pc.ply");
std::stringstream buffer_pc_file;
buffer_pc_file << t_pc_file.rdbuf();

httplib::Client cliSendFiles("http://ip::port");
httplib::MultipartFormDataItems items = {
        {"files", buffer_lf_img.str(), "lf.png", "application/octet-stream"},
        {"files", buffer_pc_file.str(), "truck.ply", "application/octet-stream"},
};
auto resSendFiles = cliSendFiles.Post("/uploadfile", items);

关于c++ - 使用cpp-httplib上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64480176/

相关文章:

c++ - 转换为 Type&& 运算符的类型

c++ - 如何在终端上以星号(*)的形式显示输入密码

c++ - StringCbCat 和 StringCchCat API 之间有什么区别

c++ - Hypodermic - OnActivating

http - 如何在 HTTP1.1 中进行标准的符合 GET 查询

android - 如何使用字节数组上传图像

php - PHP 如何使用 header() 函数设置 HTTP header ?

perl - 方法名称中发现无效字符。 HTTP 方法名称必须是 token ,即使使用 http 请求也仍然存在

php - 无法读取原始 POST 正文数据 - 在此示例中如何执行此操作?

php - HTTP Post 不向数据库发送数据?