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 can i send file over http with Qt?
Qt 6.11 is out! See what's new in the release blog

How can i send file over http with Qt?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 439 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.
  • Y Offline
    Y Offline
    Yaldiz
    wrote on last edited by
    #1

    Hi,

    I have .bin file and i want to upload this file via http i tried following code, but not happened anything.

    QFile* file = new QFile("C:/Users/Hp/Desktop/FileTest/2022-12-16 15-09-21.bin");
    QHttpMultiPart * multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    QHttpPart filePart;
    
    file->open(QIODevice::ReadOnly);
    
    filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/zip")); //or whatever type of your file.
    filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"bin\""));
    filePart.setHeader(QNetworkRequest::ContentLengthHeader, file->size());
    file->setParent(multiPart);
    
    filePart.setBodyDevice(file);
    multiPart->append(filePart);
    
    QNetworkAccessManager mgr;
    QNetworkRequest req(QUrl("some/url"));
    //req.setRawHeader("Authorization", ("Token <token>"));
    [[maybe_unused]] QNetworkReply * reply = mgr.post(req, multiPart);
    

    Thanks for helps.

    JonBJ 1 Reply Last reply
    0
    • Y Yaldiz

      Hi,

      I have .bin file and i want to upload this file via http i tried following code, but not happened anything.

      QFile* file = new QFile("C:/Users/Hp/Desktop/FileTest/2022-12-16 15-09-21.bin");
      QHttpMultiPart * multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
      QHttpPart filePart;
      
      file->open(QIODevice::ReadOnly);
      
      filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/zip")); //or whatever type of your file.
      filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"bin\""));
      filePart.setHeader(QNetworkRequest::ContentLengthHeader, file->size());
      file->setParent(multiPart);
      
      filePart.setBodyDevice(file);
      multiPart->append(filePart);
      
      QNetworkAccessManager mgr;
      QNetworkRequest req(QUrl("some/url"));
      //req.setRawHeader("Authorization", ("Token <token>"));
      [[maybe_unused]] QNetworkReply * reply = mgr.post(req, multiPart);
      

      Thanks for helps.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Yaldiz said in How can i send file over http with Qt?:

      but not happened anything.

      Did you check what the reply object returned had to say? Did you receive the QNetworkAccessManager/QNetworkReply::finished signals? What about the file->open() call just to be sure? Is the Url http and not https?

      SGaistS 1 Reply Last reply
      2
      • JonBJ JonB

        @Yaldiz said in How can i send file over http with Qt?:

        but not happened anything.

        Did you check what the reply object returned had to say? Did you receive the QNetworkAccessManager/QNetworkReply::finished signals? What about the file->open() call just to be sure? Is the Url http and not https?

        SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Beside @JonB suggestions, what is the lifetime of mgr ?
        You also don't do any error checks which is a bad idea.

        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
        2

        • Login

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