在Qt 5.6中错误地下载了文件大小

时间:2017-09-13 13:43:42

标签: c++ windows qt qfile qnetworkreply

我使用了类似的DownloadManager类,但问题是对于大文件我有一个错误的大小65536KB而不是51328022字节。 saveDisk方法有问题。

ps:我使用Qt 5.6因为我需要在Windows Vista上运行应用程序。 XP

 ng serve 

2 个答案:

答案 0 :(得分:0)

您应该通过信号QNetworkReply :: readyRead

调用DownloadManager :: saveToDisk
void 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;

我写了几个块而不是一个大块