qt - Download big files with QNetworkreply::readAll freeze for a few seconds -
i used example doc http://doc.qt.io/qt-4.8/qnetworkaccessmanager.html
i create startdownload
:
connect(pushbutton, signal(clicked(bool)), this, slot(startdownload(bool)));
in startdownload(bool)
put this:
file = new qfile("c:/foo/bar/bigfile.7z"); file->open(qiodevice::writeonly); qnetworkrequest request; request.seturl(qurl("http://localhost/bigfile.7z")); request.setrawheader("user-agent", "myownbrowser 1.0"); qnetworkreply *reply = manager->get(request); connect(reply, signal(readyread()), this, slot(slotreadyread())); connect(reply, signal(error(qnetworkreply::networkerror)), this, slot(sloterror(qnetworkreply::networkerror))); connect(reply, signal(sslerrors(qlist<qsslerror>)), this, slot(slotsslerrors(qlist<qsslerror>)));
in slotreadyread
put this:
file->write(reply->readall());
but when download arrives @ end there small freezing 2 seconds , returns normal , download complete. problem occurs if file i'm trying transfer large.
this expected behavior. qiodevice::readall()
block thread until download completes. it's possible qfile::write()
may block depending on disk speed , caching policy. readall()
method may consume quite bit of ram if file large enough.
the simplest solution download file in smaller chunks using read()
instead of readall()
.
now said, there's no easy way find perfect buffer size read network , write disk; it's going depend on how network connection responds vs. disk write speed.
Comments
Post a Comment