Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. I spend all the RAM downloading a file
Forum Updated to NodeBB v4.3 + New Features

I spend all the RAM downloading a file

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 863 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    leonardogsu
    wrote on last edited by
    #1

    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();
    }
    
    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      4
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by A Former User
        #3

        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.

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved