Download one file at the time with source code below is easy, and also download multiply file also isn't problem when using http://doc.qt.io/qt-4.8/QQueue, but downloading files in parallel i can't get it!
Assume we have:
I make code shorter and remove other codes for demonstration purpose ;)
downloader.h
#ifndef DOWNLOADER_H
#define DOWNLOADER_H
#include <QObject>
#include <QFile>
#include <QFileInfo>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
{
Q_OBJECT
public:
explicit Dowloader
(QObject *parent
= 0);
public slots:
void theDownload
(const QString &urlLink
);
private slots:
void startRequest
(const QUrl &url
);
void httpReadyRead();
void httpFinished();
private:
QNetworkAccessManager manager;
QNetworkReply *reply;
};
#endif // DOWNLOADER_H
#ifndef DOWNLOADER_H
#define DOWNLOADER_H
#include <QObject>
#include <QFile>
#include <QFileInfo>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
class Dowloader : public QObject
{
Q_OBJECT
public:
explicit Dowloader(QObject *parent = 0);
public slots:
void theDownload(const QString &urlLink);
private slots:
void startRequest(const QUrl &url);
void httpReadyRead();
void httpFinished();
private:
QNetworkAccessManager manager;
QNetworkReply *reply;
QFile *file;
};
#endif // DOWNLOADER_H
To copy to clipboard, switch view to plain text mode
downloader.cpp
#include "dowloader.h"
Dowloader
::Dowloader(QObject *parent
) :{}
void Dowloader
::theDownload(const QString &urlLink
) {
QString fileName
= fInfo.
fileName();
if(fileName.isEmpty())
fileName = "download";
file = new QFile(fileName
);
delete file;
file = 0;
return;
}
startRequest(url);
}
void Dowloader::replyMetaDataChanged()
{}
void Dowloader
::startRequest(const QUrl &url
) {
QNetworkRequest request(url);
reply = manager.get(request);
// conncet signals
connect(reply, SIGNAL(metaDataChanged()), this, SLOT(replyMetaDataChanged()));
connect(reply, SIGNAL(readyRead()), this, SLOT(httpReadyRead()));
connect(reply, SIGNAL(finished()), this, SLOT(httpFinished()));
}
void Dowloader::httpReadyRead()
{
if(file) file->write(reply->readAll());
}
void Dowloader::httpFinished()
{
file->flush();
file->close();
reply->deleteLater();
reply = 0;
delete file;
file = 0;
}
#include "dowloader.h"
Dowloader::Dowloader(QObject *parent) :
QObject(parent)
{}
void Dowloader::theDownload(const QString &urlLink)
{
QUrl url(urlLink);
QFileInfo fInfo(url.path());
QString fileName = fInfo.fileName();
if(fileName.isEmpty())
fileName = "download";
file = new QFile(fileName);
if(!file->open(QIODevice::WriteOnly)){
delete file;
file = 0;
return;
}
startRequest(url);
}
void Dowloader::replyMetaDataChanged()
{}
void Dowloader::startRequest(const QUrl &url)
{
QNetworkRequest request(url);
reply = manager.get(request);
// conncet signals
connect(reply, SIGNAL(metaDataChanged()), this, SLOT(replyMetaDataChanged()));
connect(reply, SIGNAL(readyRead()), this, SLOT(httpReadyRead()));
connect(reply, SIGNAL(finished()), this, SLOT(httpFinished()));
}
void Dowloader::httpReadyRead()
{
if(file) file->write(reply->readAll());
}
void Dowloader::httpFinished()
{
file->flush();
file->close();
reply->deleteLater();
reply = 0;
delete file;
file = 0;
}
To copy to clipboard, switch view to plain text mode
The main problem here is, in the httpReadyRead() ans httpFinished() how can operate on the correct file, reply? as you see in the *.h file they are *pointer, it means when i add new file current QNetworkReply *reply, QFile *file, gonna re-initialize with new values!
What changes need in this code to make Concurrent file downloading possible? // Doesn't mean provide some code or something, just give me a scenario about it!
Thanks
Bookmarks