1 Star 0 Fork 10

LukeLee/qtbase

forked from TKG/QtBase 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
httpdownload.cpp 1.93 KB
一键复制 编辑 原始数据 按行查看 历史
unknown 提交于 2017-02-20 22:20 . init
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QFile>
#include <QDir>
#include <QMessageBox>
#include <QEventLoop>
#include "httpdownload.h"
HttpDownload::HttpDownload(QWidget *widget, QObject *parent) :
QObject(parent),m_manager(new QNetworkAccessManager(0)),m_loop(new QEventLoop){
m_widget = widget;
}
void HttpDownload::replyNewDate(){
if(m_file){
m_file->write(m_reply->readAll());
m_file->flush();
}
}
void HttpDownload::replyFinished(){
// 能通过reply读取请求的所有数据大小和吗?貌似很难,这种流式数据
m_reply->deleteLater();
m_file->close();
m_file->deleteLater();
emit finished();
}
void HttpDownload::replyError(){
m_down_size = 0;
emit finished();
}
void HttpDownload::replyProgress(qint64 recv,qint64 total){
m_down_size = total;//当数据大时,这会被重复设置
}
int HttpDownload::downloadFile(const QString &url, const QString &file, const QString &dir){
m_reply = m_manager->get(QNetworkRequest(QUrl(url+file)));
if(dir.isEmpty()){
m_file = new QFile(file);
return 0;
}else{
QDir directory(dir);
if(!directory.exists()){
directory.mkpath(dir);
}
if(dir.endsWith("/"))
m_file = new QFile(dir+file);
else
m_file = new QFile(dir+"/"+file);
}
if(!m_file->open(QIODevice::WriteOnly)){
QMessageBox::warning(0,"错误","文件打开失败",QMessageBox::Ok);
return 0;
}
connect(m_reply,SIGNAL(readyRead()),this,SLOT(replyNewDate()));
connect(m_reply,SIGNAL(finished()),this,SLOT(replyFinished()));
connect(m_reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(replyError()));
connect(m_reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(replyProgress(qint64,qint64)));
connect(this,SIGNAL(finished()),m_loop,SLOT(quit()));
m_loop->exec();
return m_down_size;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/luke47/base.git
git@gitee.com:luke47/base.git
luke47
base
qtbase
master

搜索帮助