2011-08-06 3 views
0

J'essayer ce code:Comment obtenir le nom de fichier de la chaîne de corps de la requête 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(); 

Mais sorties dans par exemple:

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 

Cela signifie que de filename="Torrent downloaded from Demonoid.com.txt" mon cede returnes Torrent d comme un nom de fichier alors qu'il doit retourner Torrent downloaded from Demonoid.com.txt. Comment réparer mon fichier télécharger HTTP demande nom de fichier analyseur?

Répondre

3

string::find Renvoie l'index du premier caractère dans la chaîne de recherche. Donc, il vous donne l'index de f dans lorsque vous recherchez cela.

Dans la ligne

size_t end_of_file_name = request->body.find("\"",found_file + 1); 

Vous devrez changer cela pour

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 " 

changer ensuite

std::string filename(request->body, found_file+10, end_of_file_name - found_file); 

Pour

std::string filename(request->body, found_file + 10, end_of_file_name - (found_file + 10)); 

Vous voudrez peut-être ajouter une autre variable pour quitter devoir ajouter 10 tout le temps aussi bien.

Questions connexes