我使用了类似的DownloadManager类,但问题是对于大文件我有一个错误的大小65536KB而不是51328022字节。 saveDisk方法有问题。
ps:我使用Qt 5.6因为我需要在Windows Vista上运行应用程序。 XP
ng serve
答案 0 :(得分:0)
您应该通过信号QNetworkReply :: readyRead
调用DownloadManager :: saveToDiskvoid DownloadManager::onDownloadReadyRead( QNetworkReply *reply )
{
...
saveToDisk( filename, reply );
...
}
void DownloadManager::saveToDisk( QNetworkReply* reply )
{
...
file.write( reply->read( reply_->bytesAvailable() ) );
...
}
答案 1 :(得分:0)
没关系。我替换了这一行:
file.write(data->readAll());
file.close();
return true;
通过
ByteArray ba = data->readAll();
int nbTries = 99;
int n = ba.size();
int idx = 0;
while (n > 0 && nbTries > 0)
{
int ret = file.write(ba.data() + idx, std::min(16*1024, n));
if (ret > 0)
{
n -= ret;
idx += ret;
}
else
{
nbTries --;
}
}
file.close();
return nbTries > 0;
我写了几个块而不是一个大块