已经出现了同样的问题,但单一答案没有帮助:Qt Download File - QNetworkAccessManager, not getting data
所以,我正在尝试下载文件:
QNetworkRequest request;
request.setUrl(QUrl(fileUrl));
QNetworkReply * reply = m_nam.get(request);
connect(reply, SIGNAL(finished()), this, SLOT(onDownloadRequestFinished()), Qt::UniqueConnection);
connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(onDownloadRequestProgress(qint64, qint64)), Qt::UniqueConnection);
在onDownloadRequestFinished
位置:
QNetworkReply * reply = qobject_cast<QNetworkReply *>(sender());
if (reply && reply->error() == QNetworkReply::NoError) {
Q_ASSERT(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 200);
qDebug() << "reply " << reply->bytesAvailable() << reply->pos() << reply->size() << reply->isReadable() << reply->openMode() << reply->isOpen();
}
插槽会打印以下内容:reply 0 0 0 true OpenMode( "ReadOnly" ) true
所以,没有数据。但是,我可以清楚地看到它确实在某处下载了某些内容。这是一个大文件,它会下载它,从onDownloadRequestProgress
判断。
重要说明:几乎相同的代码在同一台计算机上的另一个项目中工作。我试图找到差异,但到目前为止看不到。
数据在哪里?
答案 0 :(得分:0)
您是否已连接 readyRead()信号以将接收的字节写入特定文件?
我总是这样做来保存文件:
const QNetworkRequest& request = QNetworkRequest(url);
reply = qnetworkaccessmanager->get(request);
QObject::connect(reply, SIGNAL(readyRead()), this,
SLOT( readingReadyBytes() ));
然后我创建我的插槽:
void yourClass::readingReadyBytes() {
file->write(reply->read(reply->bytesAvailable()));
}