2019-10-31 17:45:17 +00:00
|
|
|
|
#ifndef DOWNLOADER_H
|
|
|
|
|
#define DOWNLOADER_H
|
|
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
|
|
|
|
|
|
class Downloader : public QObject {
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
enum EndCode{
|
|
|
|
|
codeSuccess,
|
|
|
|
|
codeDownloading,
|
2019-10-31 19:33:18 +00:00
|
|
|
|
codeAbort,
|
2019-10-31 17:45:17 +00:00
|
|
|
|
codeFailed
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
// 从url下载数据,写入到filename文件中,如果filename为空字符串,则保存在内存中,可通过 data() 获取。
|
|
|
|
|
Downloader();
|
|
|
|
|
~Downloader();
|
|
|
|
|
|
2019-10-31 19:33:18 +00:00
|
|
|
|
void run(QNetworkAccessManager* nam, const QString& url, const QString& sid, const QString& filename);
|
2019-10-31 17:45:17 +00:00
|
|
|
|
void abort();
|
2019-10-31 19:33:18 +00:00
|
|
|
|
// void reset();
|
2019-10-31 17:45:17 +00:00
|
|
|
|
QByteArray& data(){return m_data;}
|
|
|
|
|
|
|
|
|
|
EndCode code() {return m_code;}
|
|
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
|
void _on_data_ready(); // 有数据可读了,读取并写入文件
|
|
|
|
|
void _on_finished(); // 下载结束了
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QString m_filename;
|
|
|
|
|
QFile m_file;
|
|
|
|
|
QByteArray m_data;
|
|
|
|
|
|
|
|
|
|
QNetworkReply* m_reply;
|
|
|
|
|
|
|
|
|
|
EndCode m_code;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
typedef struct DownloadParam {
|
|
|
|
|
QString url;
|
|
|
|
|
QString sid;
|
|
|
|
|
QString fname;
|
|
|
|
|
}DownloadParam;
|
|
|
|
|
|
|
|
|
|
#endif
|