Qt,QNetworkAccess Manager经常下载大文件失败

时间:2015-07-26 09:42:58

标签: qt qnetworkaccessmanager qnetworkreply

我使用以下代码通过LAN下载一些视频文件。

    QString url = "http://192.168.1.100/disk/IPCAMERA/" + downloadlist[downloadID];
    QNetworkRequest newRequest(url);
    QString concatenated = "admin:admin";
    QByteArray data = concatenated.toLocal8Bit().toBase64();
    QString headerData = "Basic " + data;
    newRequest.setRawHeader("Authorization", headerData.toLocal8Bit());
    newRequest.setUrl(url);

    reply = networkManager->get(newRequest);
    connect(reply, SIGNAL(finished()), this, SLOT(refStateChanged()), Qt::UniqueConnection);
    connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(replyDownloadProgress(qint64, qint64)));

然而,下载经常失败,当触发回复结束信号时,它实际上并没有完成下载文件而只是部分文件。我该怎么做才能解决这个问题?

2 个答案:

答案 0 :(得分:0)

您似乎应首先获得文件大小

QNetworkReply * reply = manager-> head(newRequest);

答案 1 :(得分:0)

每个回复都是Bytearray的一部分,你必须添加这个连接:

connect(reply, SIGNAL(readyRead()),this, SLOT(SlotReadyRead()));
void ClassName::SlotReadyRead()
{
if (_file)   //you must define QFile _file........
_file->write(_reply->readAll());
}

然后,您将下载完整的文件。 祝你好运!