非交互性是因为re->readAll()
在大小未知的设备上是阻塞调用。它将继续读取,直到请求完成。
大文件的问题与保存文件的字节数组的增长有关。在某些时候,你的字节数组将是 400MB,然后它必须增长到 2 倍的大小,所以你必须一次保存 ~1GB,分成两个大块,并且由于地址空间碎片和分配请求将失败,您的程序崩溃。
对代码的微小更改会产生所需的行为:您立即开始阅读和写作,然后将两者联系起来:
class MainWindow {
...
// You shouldn't be creating a new network access manager for each request.
QScopedPointer<QNetworkAccessManager> m_nam;
...
};
void MainWindow::handleError(const QNetworkReply *) { ... }
void MainWindow::handleError(const QFile *) { ... }
void MainWindow::on_startButton_clicked()
{
// Lazily create the network access manager once.
if (!m_nam) m_nam.reset(new QNetworkAccessManager);
// The file is destructed, and thus flushed and closed, when the
// last shared pointer to this file is destructed.
QSharedPointer<QFile> output(new QFile("C:\\a.bin"));
if (!output->open(QIODevice::WriteOnly)) return;
QNetworkReply *reply = m_nam->get(QNetworkRequest(QUrl("http://somesite/ai/iai.jpg")));
// The lambda syntax creates a functor object that holds a copy
// of the reply pointer and the output file pointer.
// This functor will be destructed when the reply is destructed.
QObject::connect(reply, &QIODevice::readyRead, [this, output, reply]{
QByteArray data(reply->bytesAvailable(), Qt::Uninitialized);
qint64 in = reply->read(data.data(), data.size());
qint64 out = output->write(in);
if (in < 0) {
handleError(reply);
reply->deleteLater();
}
else if (out != in) {
handleError(output.data());
reply->deleteLater();
}
});
// The reply will self-destruct when finished, thus deleting the file
// instance.
QObject::connect(reply, &QNetworkReply::finished, reply, &QObject::deleteLater);
}