Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

I spend all the RAM downloading a file



  • Hi,
    Im new in QT and when I try to download a file (9GB) I spend all my RAM, I would like to know how can I store the temporaly file in the hard drive instead to the RAM. If the size of the file is smaller than my system available RAM, I be able to download it.
    Im using the code from bogotobongo
    thanks!

    //
    #ifndef DOWNLOADER_H
    #define DOWNLOADER_H
    
    #include <QObject>
    #include <QNetworkAccessManager>
    #include <QNetworkRequest>
    #include <QNetworkReply>
    #include <QUrl>
    #include <QDateTime>
    #include <QFile>
    #include <QDebug>
    
    
    class Downloader : public QObject
    {
        Q_OBJECT
    public:
        explicit Downloader(QObject *parent = 0);
    
        void doDownload();
    
    signals:
    
    public slots:
        void replyFinished (QNetworkReply *reply);
    
    private:
       QNetworkAccessManager *manager;
    
    };
    
    //void Downloader::doDownload()
    {
        manager = new QNetworkAccessManager(this);
    
        connect(manager, SIGNAL(finished(QNetworkReply*)),
                this, SLOT(replyFinished(QNetworkReply*)));
    
        manager->get(QNetworkRequest(QUrl("http://bogotobogo.com")));
    }
    
    //void Downloader::replyFinished (QNetworkReply *reply)
    {
        if(reply->error())
        {
            qDebug() << "ERROR!";
            qDebug() << reply->errorString();
        }
        else
        {
            qDebug() << reply->header(QNetworkRequest::ContentTypeHeader).toString();
            qDebug() << reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toString();;
            qDebug() << reply->header(QNetworkRequest::ContentLengthHeader).toULongLong();
            qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
            qDebug() << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
    
            QFile *file = new QFile("C:/Qt/Dummy/downloaded.txt");
            if(file->open(QFile::Append))
            {
                file->write(reply->readAll());
                file->flush();
                file->close();
            }
            delete file;
        }
    
        reply->deleteLater();
    }
    


  • Hi @leonardogsu,

    Connect to the QNetworkReply::downloadProgress() signal, and write (append) the available bytes to a temporary file in that slot. Then use that temporary file however you like once QNetworkReply::finished() is emitted.

    Cheers.



  • Hi! The following...

     manager->get(QNetworkRequest(QUrl("http://bogotobogo.com")));
    

    ... returns a QNetworkReply *. QNetworkReply inherits from QIODevice; and you'd usually use [signal] void QIODevice::readyRead() and [virtual] qint64 QIODevice::bytesAvailable() const to react to incoming data.

    Edit: The good old Fortune Client Example shows how to do it; it uses a QTcpSocket but that makes no difference as both, QTcpSocket and QNetworkReply, are QIODevices.


Log in to reply