You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.1 KiB
C++

#include <iostream>
#include <fstream>
#include "../cpp-httplib/httplib.h"
#include <direct.h>
using namespace std;
using namespace httplib;
string GetExeDir()
{
char buff[_MAX_PATH];
_getcwd(buff, _MAX_PATH);
string currPath(buff);
return string(buff);
}
const string CLIENT_FILE_DIR = "/ClientFiles/";
void download_file(const string& file_name) {
httplib::Client cli("http://localhost:1234");
auto res = cli.Get(("/download/" + file_name).c_str());
if (res && res->status == 200) {
string save_path = GetExeDir()+CLIENT_FILE_DIR + file_name;
// ´ò¿ªÎļþ½øÐÐдÈë
ofstream file(save_path, ios::binary);
if (!file) {
cerr << "Failed to save file: " << save_path << endl;
return;
}
file.write(res->body.c_str(), res->body.size());
file.close();
cout << "File downloaded and saved at: " << save_path << endl;
}
else {
cerr << "Failed to download file. Status code: "
<< (res ? to_string(res->status) : "No response") << endl;
}
}
int main() {
string file_name;
while (file_name!="q")
{
cout << "Enter the file name to download (q to Exit): ";
cin >> file_name;
download_file(file_name);
}
system("pause");
return 0;
}