SHARE
TWEET

Qt POST

a guest Jul 31st, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     QFile file("/path/file.jpg");
  2.     if(file.open(QIODevice::ReadOnly))
  3.     {
  4.         QUrl url("http://localhost:8888");
  5.  
  6.         QNetworkRequest request;
  7.         request.setUrl(url);
  8.  
  9.         QHttpMultiPart multipart(QHttpMultiPart::FormDataType);
  10.  
  11.         QHttpPart image;
  12.         image.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("multipart/form-data"));
  13.         image.setHeader(QNetworkRequest::ContentLengthHeader, QVariant(file.size()));
  14.         image.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("name=\"image\"; filename=\"name.jpg\""));
  15.         image.setBodyDevice(&file);
  16.  
  17.         multipart.append(image);
  18.  
  19.         QNetworkReply *reply = manager->post(request, &multipart);
  20.  
  21.         QEventLoop wait;
  22.         connect(reply, &QNetworkReply::finished, [&](){
  23.             if(reply->error() == QNetworkReply::NoError)
  24.                 qDebug() << reply->readAll();
  25.  
  26.             reply->deleteLater();
  27.             wait.quit();
  28.  
  29.         });
  30.         wait.exec();
  31.     }
  32.     file.close();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top