PDA

View Full Version : What the best way to trigger a download from a webpage using QWebEngine?



ayanda83
14th December 2016, 08:04
I am using QWebEngine to mine some HTML data from a list of websites, some of this data is pdf documents that must be downloaded from the webpage. I would like to know if there is a way to programmatically trigger a download of a pdf document onto a local file system. If it is possible, is there a way to avoid pausing program execution while the downloading is in progress but instead continue to another webpage where I can initiate another download. The plan is to have 5 downloads running at a time and the rest will be queued. I am not looking for code, i just would like to know how you would approach this problem. Thanking you in advance.

anda_skoa
14th December 2016, 11:39
If you can get the URL of the PDF you could just download it with a separate QNetworkAccessManager.

If the download is not provided as a link, you'll probably need to trigger the download action via script.
The QWebEngineProfile seems to have a signal that could be useful for managing downloads by the webegine itself, which it will highly likely do in the background automatically.

Cheers,
_

ayanda83
14th December 2016, 13:13
Thank you for your reply. Below is a piece of code I wrote prior to your reply but it doesn't really work the way I want it. I can retrieve the pdf url's just fine, using QWebEnginePage::runJavaScript() but I haven't use QNetworkAccessManager before and I don't really know how to handle the download inside the callback function.
javaScript_2.append("function downloadAttachment(){"
"var attachmentTag = document.querySelectorAll(\"div.detailsValue > a\");"
"var downloadLinks = [];"
"var k;"
"for(k = 0; k < attachmentTag.length; k++){"
"downloadLinks[k] = attachmentTag[k].getAttribute(\"href\");"
"}"
"return downloadLinks;"
"}"
"downloadAttachment();");

runJavaScript(javaScript_2, [&](const QVariant downloadLinks){
QStringList dLinks = downloadLinks.toStringList();

QNetworkAccessManager nManager;
QNetworkReply *reply;

for(int i = 0; i < dLinks.size(); i++)
{
QNetworkRequest request(QUrl(QString("http://web1.capetown.gov.za%1").arg(dLinks.at(i))));
reply = nManager.get(request);
}

});

anda_skoa
15th December 2016, 11:39
First you need to have the QNetworkAccessManager either as a member or on the heap, because you don't want it to be deleted when your function's scope ends.

For the download handling, connect to the QNetworkReply's readyRead() signal and the finished() signal (or just the finished signal if the files aren't that big).

My recommendation would be to have a Download task object that gets the QNetworkReply object and then handles everything for that single download.

Cheers,
_