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. [Solved] Downloading large files

[Solved] Downloading large files

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.3k Views
  • 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.
  • T Offline
    T Offline
    tomi35
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      t3685
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tomi35
        wrote on last edited by
        #3

        [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.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tomi35
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

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