Downloading a file with Qt?

2019-02-28 23:01发布

问题:

I am trying to figure out a way for my Qt browser app can download a word document from our web app. The web app is written in ExtJS, when (in a browser such as Chrome) a user clicks the "Download report" button a javascript event listener detects that click and opens a 0px0px iframe and the file downloads. I'm not sure how to replicate this browser feature in Qt?

When I click on the link I get a network reply of "Operation canceled" 5?

What class/method would be best to retrieve these files?

回答1:

to download a file you need : a QNetworkAccessManager in this case http. a QFile in this case file. a QNetworkReply in this case reply connect the reply with a slot that writes the bytes received through QNetworkAccessManager in this case the slot is called readingReadyBytes()

so i create the request and connect to my slot:

 const QNetworkRequest& request = QNetworkRequest(url);
 reply = http->get(request);
 QObject::connect(reply, SIGNAL(readyRead()), this,
        SLOT( readingReadyBytes() ));

then i create my slot:

 void yourClass::readingReadyBytes() {
    file->write(reply->read(reply->bytesAvailable()));
 }

finally you have to save and close your file. tipically is done when the QNetworkAccessManager emit the finished signal.. this is what i remember and i think it is all..