1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| int upload(const string& file_path, const string& token,string upload_path,const string& alist_host ) {
httplib::Client cli(alist_host); httplib::Headers headers = { {"Authorization", token}, {"File-Path", upload_path}, {"As-Task", "true"}, {"Content-Type", "application/octet-stream"} };
ifstream file(file_path, ios::binary); if (!file) { cerr << "无法打开文件: " << file_path << endl; return 1; }
string body((istreambuf_iterator<char>(file)), istreambuf_iterator<char>());
headers.insert({"Content-Length", to_string(body.size())});
auto res = cli.Put("/api/fs/put", headers, body, "application/octet-stream"); if (res) { cout << "状态码: " << res->status << endl; cout << "响应内容: " << res->body << endl; } else { cerr << "上传失败,未收到响应。" << endl; }
return 0; }
|