0

已经有同样的问题,但单一的答案没有帮助: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.

重要说明:几乎相同的代码在同一台计算机上的另一个项目中工作。我试图找到差异,但到目前为止还没有看到。

数据在哪里?

4

1 回答 1

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()));
 }
于 2013-11-15T14:24:46.783 回答