PDA

View Full Version : Downloading Pdf's from the web using QNetworkAccessManager.



ayanda83
6th January 2017, 06:43
I've got a program that is supposed to download a list of pdf documents from the web using QNetworkAccessManager. I have a QStringList of pdf urls and my problem is getting QNetworkAccessManager to download 6 pdf's at a time, when I try implementing this, the program keeps downloading the first url on the list 6 times. What could be causing this?
void CPT_Page::getPdf(QStringList linksList)
{
qDebug() << linksList <<endl;
for(int i = 0; i < linksList.size(); i++)
{
download_request_list.append(QNetworkRequest(QUrl( QString("http://web1.capetown.gov.za%1").arg(linksList.at(i)))));
download_replies_list.append(nManager->get(download_request_list.at(i)));
connect(download_replies_list.at(i), SIGNAL(finished()), this, SLOT(downloadAttachment()));
}

}

void CPT_Page::downloadAttachment()
{
static int fileNum = 1;
QFile tempFile(QString("C:/Users/C5248134/Desktop/Projects/Ithala/Include/Sourced_Tenders/temp%1.pdf").arg(fileNum));
fileNum++;

if(!tempFile.open(QFile::WriteOnly))
qDebug() << "File did not open" <<endl;
QDataStream outStream(&tempFile);

outStream << download_replies_list.at(0)->readAll() <<endl;
download_replies_list.clear();
}

anda_skoa
6th January 2017, 10:06
Line 23 always accesses index 0, i.e. the first download reply.

Maybe just connect to the QNAM's finished signal instead of storing the replies in a list?
Or do yoo need to cancel them?

Cheers,
_

ayanda83
6th January 2017, 10:52
thank you for your reply. just one question though. Can the QNM objects handle multiple different requests at a time. I am asking because even when I connect the QNM finished signal to CPT_Page::downloadAttachment() slot, I still get the same pdf document multiple time. I guess what I am asking is that can QNM::get() perform another request while it hasn't finished with the first request.

anda_skoa
6th January 2017, 13:47
Yes, QNAM handles multiple requests concurrently.
I think the limit is 6 per host, but you can enqueue many more.

Cheers,
_