0
我使用了类似的DownloadManager类,但问题是,对于大文件我有一个错误的大小65536KB而不是51328022字节。 saveDisk方法有问题。错误dontloaded文件大小在Qt 5.6
PS:我用的Qt 5.6因为我需要在Windows Vista上运行应用程序& XP
bool DownloadManager::saveToDisk(const QString &filename, QIODevice *data)
{
LogManager* logmgr = LogManager::GetInstance();
FileManager* filemgr = FileManager::GetInstance();
QFileInfo fileinfo(filename);
QString dirpath = fileinfo.absoluteDir().absolutePath();
if (!filemgr->DirectoryIsPresent(dirpath))
{
if (!filemgr->CreateDirectory(dirpath)) {
logmgr->Log(LOG_LEVEL_ERROR, QString("cannot create directory (") + dirpath + ")");
}
}
QFile file(filename);
if (!file.open(QIODevice::WriteOnly)) {
const QString errorText = QString("Could not open ") + filename + QString(" for writing:") + file.errorString();
logmgr->Log(LOG_LEVEL_ERROR, errorText);
return false;
}
file.write(data->readAll());
file.close();
return true;
}
void DownloadManager::downloadFinished(QNetworkReply *reply)
{
LogManager* logmgr = LogManager::GetInstance();
if (m_currentDownloads.contains(reply))
{
QUrl url = reply->url();
if (reply->error() != QNetworkReply::NoError) {
m_nbFailedDownload++;
const QString errorText = QString("Download of ")+ url.toString() +" failed: " + reply->errorString() + " (" + QString::number(reply->error()) + ")";
logmgr->Log(LOG_LEVEL_ERROR, errorText);
} else {
m_nbSucceededDownload++;
QString filename = saveFileName(url);
if (saveToDisk(filename, reply))
{
const QString infoText = QString("Download of ") + url.toString() + " succeeded (saved to " + filename + ")";
logmgr->Log(LOG_LEVEL_INFO, infoText);
}
}
m_currentDownloads.removeAll(reply);
reply->deleteLater();
}
int total = m_nbTotalDownload == 0? 1:m_nbTotalDownload;
emit onProgress((m_nbFailedDownload + m_nbSucceededDownload) * 100/total);
if (m_currentDownloads.isEmpty()){
logmgr->Log(LOG_LEVEL_INFO, "DownloadManager downloads finished");
emit onFinished();
}
}
你甚至不试图检查'file.write'写的所有数据块数。阅读函数做什么以及它们的返回值是什么意思。 – nwp