使用Qt下载文件? [英] Downloading a file with Qt?
问题描述
我想找出一个方法,我的Qt浏览器应用程序可以从我们的网络应用程序下载一个word文档。网络应用程序是用ExtJS编写的,当用户点击下载报告按钮(在Chrome等浏览器中)时,javascript事件侦听器检测到该点击并打开一个0px0px的iframe和文件下载。我不知道如何在Qt中复制此浏览器功能。
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?
当我点击链接,我得到一个网络回复操作已取消5
?
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?
推荐答案
下载文件: QNetworkAccessManager 在这种情况下 http 。
a QFile 在这种情况下文件。
a 在这种情况下
在这种情况下将回复连接到通过 QNetworkAccessManager 该槽位称为 readingReadyBytes()
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() ));
然后我创建我的位置:
void yourClass::readingReadyBytes() {
file->write(reply->read(reply->bytesAvailable()));
}
最后,您必须保存并关闭文件。显然是当 QNetworkAccessManager 发出完成的信号时。
这是我记得,我认为这一切。
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..
这篇关于使用Qt下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!