如何从http请求正文字符串中获取文件名?
所以我尝试这样的代码:
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
作为文件名,而它应该返回从 Demonoid.com.txt 下载的 Torrent
。如何修复我的文件上传 http 请求文件名解析器?
So I try such code:
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();
But it outputs in for example:
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
This means that from filename="Torrent downloaded from Demonoid.com.txt"
my cede returnes Torrent d
as a file name while it should return Torrent downloaded from Demonoid.com.txt
. How to fix my file upload http request filename parser?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
string::find
返回搜索字符串中第一个字符的索引。因此,当您搜索时,它会为您提供filename=
中f
的索引。在行中,
您必须将其更改为“
然后更改
为”。
您可能需要添加另一个变量,以避免一直添加
10
。string::find
returns the index of the first character in the search string. So it's giving you the index of thef
infilename=
when you search for that.In the line
You'll have to change that to
Then change
To
You might want to add another variable to quit having to add
10
all the time as well.