如何从http请求体字符串获取文件名
How to get filename from http request body string?
所以我尝试这样的代码:
std::ofstream myfile;
myfile.open ("example.txt", std::ios_base::app );
myfile << "Request body: " << request->body << std::endl << "Request size: " << request->body.length() << std::endl;
size_t found_file = request->body.find("filename=");
if (found_file != std::string::npos)
{
size_t end_of_file_name = request->body.find(""",found_file + 1);
if (end_of_file_name != std::string::npos)
{
std::string filename(request->body, found_file+10, end_of_file_name - found_file);
myfile << "Filename == " << filename << std::endl;
}
}
myfile.close();
但是它输出在例如:
Request body: ------WebKitFormBoundary0tbfYpUAzAlgztXL
Content-Disposition: form-data; name="datafile"; filename="Torrent downloaded from Demonoid.com.txt"
Content-Type: text/plain
Torrent downloaded from http://www.Demonoid.com
------WebKitFormBoundary0tbfYpUAzAlgztXL--
Request size: 265
Filename == Torrent d
这意味着从filename="Torrent downloaded from Demonoid.com.txt"
我的cede返回Torrent d
作为文件名,而它应该返回Torrent downloaded from Demonoid.com.txt
。如何修复我的文件上传http请求文件名解析器?
string::find
返回搜索字符串中第一个字符的索引。所以当你搜索filename=
的时候它会给你f
的索引
size_t end_of_file_name = request->body.find(""",found_file + 1);
你必须把它改成
size_t end_of_file_name = request->body.find(""", found_file + 9 + 1); // 9 because that's the length of "filename=" and 1 to start at the character after the "
然后改变
std::string filename(request->body, found_file+10, end_of_file_name - found_file);
std::string filename(request->body, found_file + 10, end_of_file_name - (found_file + 10));
您可能需要添加另一个变量,以避免一直添加10
相关文章:
- 从C字符串中获取奇怪的字符串长度
- 获取字符串的长度并将其分配给数组
- 我如何获取从 C++ 到 C 的映射<字符串、int>#
- 调用CreateProcess()并获取字符串的返回值
- 从字符串中获取结构的大小
- Cryptopp:获取密码输入的填充字符串
- 获取无效的字符串大小
- 获取 OID(类型::b_oid)作为 MongoDB C++驱动程序中的字符串
- C ++编译器如何获取argv字符串的长度
- 给枚举一个整数,并在 C++ 中将其相关值作为字符串获取
- 如何使用字符串获取用户控制台输入
- 通过字符串获取小部件
- 无法从字符串获取数字输入
- 在C++的静态方法中将类名作为字符串获取
- 从设备实例路径字符串获取设备实例 DWORD
- HRESULT到字符串(获取文档路径)C++
- 如何从http请求体字符串获取文件名
- 如何使用带有垃圾字符的子字符串获取字符串
- 通过字符串获取对话框项的可能性
- 有没有办法将 WinAPI 错误标志作为字符串获取