修复小文件上传问题
This commit is contained in:
@@ -219,7 +219,7 @@ bool DownloadFileAPI_web(const std::string& strUrl, // 接口路径
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////上传文件接口
|
||||
|
||||
//处理文件上传响应
|
||||
void handleUploadResponse(const std::string& response, std::string& wavepath, int type) {
|
||||
bool handleUploadResponse(const std::string& response, std::string& wavepath, int type) {
|
||||
|
||||
using nlohmann::json; //把 nlohmann::json 这个名字带到当前作用域
|
||||
|
||||
@@ -231,27 +231,34 @@ void handleUploadResponse(const std::string& response, std::string& wavepath, in
|
||||
catch (const json::parse_error& e) {
|
||||
std::cerr << "Error parsing response: " << e.what() << std::endl;
|
||||
DIY_ERRORLOG_CODE("process",0,LOG_CODE_JSON, "文件上传接口响应异常,上传文件失败");
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 提取字段
|
||||
if (!json_data.contains("code") || !json_data.contains("data")) {
|
||||
std::cerr << "Error: Missing expected fields in JSON response." << std::endl;
|
||||
DIY_ERRORLOG_CODE("process",0,LOG_CODE_JSON, "文件上传接口响应异常,上传文件失败");
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string code = json_data["code"].get<std::string>();
|
||||
std::cout << "Response Code: " << code << std::endl;
|
||||
|
||||
std::string msg = json_data.value("msg", std::string{"not found"});
|
||||
std::string msg = json_data.value("message",
|
||||
json_data.value("msg", std::string{"not found"}));
|
||||
std::cout << "Message: " << msg << std::endl;
|
||||
|
||||
if (code != "A0000") {
|
||||
std::cerr << "File upload failed, response code=" << code
|
||||
<< ", message=" << msg << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
auto& data = json_data["data"];
|
||||
if (!data.contains("name") || !data.contains("fileName") || !data.contains("url")) {
|
||||
std::cerr << "Error: Missing expected fields in JSON data object." << std::endl;
|
||||
DIY_ERRORLOG_CODE("process",0,LOG_CODE_JSON, "文件上传接口响应异常,上传文件失败");
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string name = data["name"].get<std::string>();
|
||||
@@ -288,20 +295,24 @@ void handleUploadResponse(const std::string& response, std::string& wavepath, in
|
||||
|
||||
std::cout << "wavepath: " << wavepath << std::endl;
|
||||
DIY_INFOLOG_CODE("process",0,LOG_CODE_FILE, "上传文件成功,远端文件名:%s", wavepath.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
//上传文件
|
||||
void SendFileWeb(const std::string& strUrl, const std::string& localpath, const std::string& cloudpath, std::string& wavepath, int type) {
|
||||
bool SendFileWeb(const std::string& strUrl, const std::string& localpath, const std::string& cloudpath, std::string& wavepath, int type) {
|
||||
|
||||
wavepath.clear();
|
||||
|
||||
// 基本存在性检查
|
||||
if (access(localpath.c_str(), F_OK) != 0) {
|
||||
std::cerr << "Local file does not exist: " << localpath << std::endl;
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
// ★新增:stat 打印大小,便于快速确认读源
|
||||
struct stat st {};
|
||||
if (stat(localpath.c_str(), &st) != 0) {
|
||||
perror("stat");
|
||||
return false;
|
||||
} else {
|
||||
std::cout << "[debug] upload file: " << localpath
|
||||
<< ", size=" << static_cast<long long>(st.st_size) << " bytes\n";
|
||||
@@ -359,6 +370,7 @@ void SendFileWeb(const std::string& strUrl, const std::string& localpath, const
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply_web);
|
||||
|
||||
// 执行请求
|
||||
bool upload_ok = false;
|
||||
CURLcode res = curl_easy_perform(curl);
|
||||
if (res != CURLE_OK) {
|
||||
const char* em = errbuf[0] ? errbuf : curl_easy_strerror(res);
|
||||
@@ -366,16 +378,18 @@ void SendFileWeb(const std::string& strUrl, const std::string& localpath, const
|
||||
DIY_ERRORLOG_CODE("process",0,LOG_CODE_CONFIG, "通过文件接口上传文件 %s 失败",localpath.c_str());
|
||||
} else {
|
||||
std::cout << "http web success, response: " << resPost0 << std::endl;
|
||||
handleUploadResponse(resPost0, wavepath, type); // 处理响应
|
||||
upload_ok = handleUploadResponse(resPost0, wavepath, type); // 处理响应
|
||||
}
|
||||
|
||||
// 清理
|
||||
curl_formfree(formpost); // 释放表单数据
|
||||
//curl_slist_free_all(header_list); // 释放头部列表
|
||||
curl_easy_cleanup(curl);
|
||||
return upload_ok;
|
||||
} else {
|
||||
std::cerr << ">>> curl init failed" << std::endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//上传暂态文件
|
||||
@@ -1851,4 +1865,4 @@ void process_qvvr_async_tasks()
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user