Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to upload screenshot through http multipart ?
Forum Updated to NodeBB v4.3 + New Features

How to upload screenshot through http multipart ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 212 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    animeshdhakal
    wrote on 18 Jul 2022, 11:45 last edited by
    #1

    I am trying to upload a screenshot to http server. Here is my current code but it doesn't work

    QNetworkAccessManager* manager = new QNetworkAccessManager();
    QNetworkRequest request;
    
    QByteArray data;
    QBuffer buffer(&data);
    
    QScreen* screen = QGuiApplication::primaryScreen();
        
    QPixmap pixmap = screen->grabWindow(0);
    
    buffer.open(QIODevice::WriteOnly);
    
    pixmap.save(&buffer, "PNG");
    
    QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    
    QHttpPart imagePart;
    imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png"));
    imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"image\"; filename=\"screenshot.png\""));
    imagePart.setBodyDevice(&buffer);
    
    multiPart->append(imagePart);
    
    QObject::connect(manager, &QNetworkAccessManager::finished, &app, [](QNetworkReply* reply){
            QString json = reply->readAll();
            qDebug() << "Json response" << json;
    });
    
    request.setUrl(QUrl("Some Upload URL"));
    
    manager->post(request, multiPart);
    
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 18 Jul 2022, 21:25 last edited by
      #2

      Hi,

      What exactly does not work ?
      What error are you getting ?
      Did you check the server's logs to see what happens there ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        animeshdhakal
        wrote on 19 Jul 2022, 01:58 last edited by
        #3

        Server is working properly. I am neither getting a response or error but when i remove the append of imagePart, i am getting a response from server.

        J 1 Reply Last reply 19 Jul 2022, 05:37
        0
        • A animeshdhakal
          19 Jul 2022, 01:58

          Server is working properly. I am neither getting a response or error but when i remove the append of imagePart, i am getting a response from server.

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 19 Jul 2022, 05:37 last edited by
          #4

          @animeshdhakal said in How to upload screenshot through http multipart ?:

          or error

          Are you sure? In the code you posted you have no error handling...

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2

          1/4

          18 Jul 2022, 11:45

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved