我正在使用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/