简体   繁体   English

使用QNetworkReply如何发布文件,然后从服务器接收转换后的文件?

[英]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. 我可以使用QNetworkReplyQNetworkAccessManagerQNetworkRequest等在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 : 您还可以通过将QNetworkAccessManagerfinished信号连接到插槽并将文件写入那里,以异步方式进行操作:

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.

相关问题 从 QNetworkReply 读取大文件到 QFile? 从 QNetworkReply 流式传输 - Reading large file from QNetworkReply to QFile? Streaming from QNetworkReply 如何从QNetworkReply中读取内容(http响应正文) - How can I read content (http response body) from a QNetworkReply 将QNetworkReply写入文件 - Writing a QNetworkReply to a file 如何从QNetworkReply读取数据? - How do I read the data from QNetworkReply? 如何从QNetworkReply读取标头 - How do I read headers from a QNetworkReply 我怎么知道 QNetWorkReply 来自哪个代理? - How do I know a QNetWorkReply comes from which proxy? 如何关闭已转换为FILE *的句柄? - How do I CloseHandle a handle that was converted to a FILE*? 使用QNetworkAccessManager从Web检索数据:已下载文件,但QNetworkReply :: readAll返回null - Retrieving data from web with QNetworkAccessManager: the file is downloaded but QNetworkReply::readAll returns null 如何从 cpp 文件发出信号并在 QML 文件中接收它? - how to emit signal from cpp file and receive it in QML file? 我如何发送(和接收)从Swift到Objective c ++到c ++的超大型浮动数组,然后再备份到Swift? - How can I send (and receive) extremely large floating arrays from Swift to Objective c++ to c++, and then back up to Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM