app_curlFunc.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #ifndef APP_CURLFUNC_H
  2. #define APP_CURLFUNC_H
  3. #include <QObject>
  4. #include <QString>
  5. #include <curl/curl.h>
  6. class CurlFtpUploader : public QObject
  7. {
  8. Q_OBJECT
  9. public:
  10. explicit CurlFtpUploader(QObject *parent = nullptr);
  11. ~CurlFtpUploader();
  12. // 设置 FTP 参数
  13. void setFtpParams(const QString &username,
  14. const QString &password,
  15. const QString &host,
  16. int port,
  17. const QString &remotePath);
  18. // 异步上传文件(非阻塞)
  19. void uploadFileAsync(const QString &localFilePath);
  20. signals:
  21. void progressChanged(double uploadedPercent); // 进度百分比 (0.0 ~ 100.0)
  22. void finished(bool success, const QString &error); // 完成信号
  23. private:
  24. // 静态回调函数(供 CURL 调用)
  25. static size_t readCallback(char *buffer, size_t size, size_t nitems, void *userdata);
  26. static int progressCallback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow);
  27. // 内部上传逻辑
  28. void performUpload(const QString &localFilePath);
  29. // FTP 参数
  30. QString m_username;
  31. QString m_password;
  32. QString m_host;
  33. int m_port;
  34. QString m_remotePath;
  35. };
  36. #endif // APP_CURLFUNC_H