[英]With QNetworkReply how can I post a file and then receive a converted file back from the server?
I can post a file in Qt using QNetworkReply
, QNetworkAccessManager
, QNetworkRequest
, etc. to a server. 我可以使用
QNetworkReply
, QNetworkAccessManager
, QNetworkRequest
等在Qt中将文件发布到服务器。 This server converts the file and spits it back. 该服务器转换文件并将其吐回。 How do I get that spitted-back file?
如何获得随身携带的文件? Do I need to find the url of the file coming back from the headers somehow and then have
QNetworkAccessManager
make a get request for it? 我是否需要以某种方式找到从标头返回的文件的url,然后让
QNetworkAccessManager
进行获取请求? Or can I somehow get it from the existing QNetworkReply
signals and write it to file? 还是我可以从现有的
QNetworkReply
信号中获取它并将其写入文件?
If the server returns the file in response to the request then you can read the file directly from the returned reply : 如果服务器响应请求而返回文件,那么您可以直接从返回的回复中读取文件:
QNetworkAccessManager nam;
QUrl url(QString("http://www.example.com"));
url.addQueryItem("parameter", "value");
QNetworkRequest request(url);
QNetworkReply *reply = nam.get(request);
QEventLoop eventloop;
connect(reply,SIGNAL(finished()),&eventloop,SLOT(quit()));
eventloop.exec();
if (reply->error() == QNetworkReply::NoError)
{
QByteArray bts = reply->readAll();
QFile file("path/to/file");
file.open(QIODevice::WriteOnly);
file.write(bts);
file.close();
}
You can also do it in an asynchronous way by connecting the finished
signal of the QNetworkAccessManager
to a slot and write the file there : 您还可以通过将
QNetworkAccessManager
的finished
信号连接到插槽并将文件写入那里,以异步方式进行操作:
connect(&nam,SIGNAL(finished(QNetworkReply*)),this,SLOT(onFinished(QNetworkReply*)));
void onFinished(QNetworkReply* reply)
{
if (reply->error() == QNetworkReply::NoError)
{
QByteArray bts = reply->readAll();
QFile file("path/to/file");
file.open(QIODevice::WriteOnly);
file.write(bts);
file.close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.