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. Uploading a file in Multipart form data in Qt5
QtWS25 Last Chance

Uploading a file in Multipart form data in Qt5

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 4.0k Views
  • 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.
  • J Offline
    J Offline
    jiji
    wrote on last edited by
    #1

    Any one can help me please, how to upload picture in Multipart form data in QT??

    the_T 1 Reply Last reply
    0
    • J jiji

      Any one can help me please, how to upload picture in Multipart form data in QT??

      the_T Offline
      the_T Offline
      the_
      wrote on last edited by
      #2

      @jiji
      See Qt5 Docs for an example
      http://doc.qt.io/qt-5/qhttpmultipart.html#details

      -- No support in PM --

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jiji
        wrote on last edited by
        #3

        I try it ,but it didn't work I got this error:
        QIODevice::read (QFile, "file:///home/Images/img.png"): device not open

        the_T 1 Reply Last reply
        0
        • J jiji

          I try it ,but it didn't work I got this error:
          QIODevice::read (QFile, "file:///home/Images/img.png"): device not open

          the_T Offline
          the_T Offline
          the_
          wrote on last edited by
          #4

          @jiji

          Can you share your code how you try to add the image as multi part?

          -- No support in PM --

          1 Reply Last reply
          1
          • J Offline
            J Offline
            jiji
            wrote on last edited by jiji
            #5

            void product::add(QString photos)
            {
            QString id=settings.value("id").toString();
            qDebug() <<"id"<<id;

            QString token=settings.value("token").toString();
            qDebug() <<"token"<<token;
            
            QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
            
            QHttpPart photo;
            photo.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/*"));
            photo.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"photo\""));
            QFile *file = new QFile(photos);
            file->open(QIODevice::ReadWrite);
            photo.setBodyDevice(file);
            file->setParent(multiPart);
            
            multiPart->append(photo);
            
            QUrl url("http://localhost:3070/auth/v1/product/Add");
            QNetworkRequest request(url);
            request.setRawHeader("x-access-token","" + token.toUtf8());
            QNetworkAccessManager manager;
            QNetworkReply *reply = manager.post(request, multiPart);
            multiPart->setParent(reply);
            

            while(!reply->isFinished())
            { qApp->processEvents(); }
            }

            1 Reply Last reply
            0

            • Login

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