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

[Solved] Downloading large files



  • Hello!

    I am trying to make a FileDownloader class for my application which is compatible with downloading large file to avoid high memory usage during download. I wrote the following function to write the file out during download using the QNetworkReply's readyRead() signal:

    @void FileDownloader::writeReady(){
    if(file->open(QIODevice::ReadWrite | QIODevice::Append)){
    QDataStream fout(file);
    fout << reply->readAll();
    } else{
    qDebug(file->errorString().toLatin1());
    }
    file->close();
    }@

    The problem with this that it's not writing the file how it supposed to be and the file gets corrupted. However if I use the finished() signal instead of the readyRead() signal, it perfectly writes out the file (it has a plus 4 byte size mistake, but I can open the file and read all of it's content perfectly).

    How can I make this compatible with readyRead() signal and large files?



  • Are you writing everything from @reply->readAll()@ to the file, is "reply" your socket?



  • [quote author="t3685" date="1398602255"]Are you writing everything from @reply->readAll()@ to the file, is "reply" your socket?[/quote]

    I think I am writing all of it, because the file size is bigger than it should be. I think it's just not write it in the correct way for some reason.

    The reply is a QNetworkReply object, which I get when I call the networkAccessManager.get(request) command.



  • Okay, I solved my problem.

    I modified the code in the following ways: I put the file opening to my download() function which starts the whole process, and added a finish slot where I flush and close the file. Then in the readyRead() signal, I'm just using file->write(reply->readAll()) to write the data to the file. It works perfectly, the file and it's size is correct, and there's only a little more memory usage during download.


Log in to reply