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. QHttpServer and large file
Forum Updated to NodeBB v4.3 + New Features

QHttpServer and large file

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 254 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.
  • M Offline
    M Offline
    mrdebug
    wrote on 11 Apr 2025, 21:51 last edited by
    #1

    My QHttpServer server has to transfer a large file.
    With a little file I can use the write() function of QHttpServerResponder object like this

    responder.write(QBABuffer, "application/octet-stream");
    

    where QBABuffer is the intere file.
    But with a large file, how can I send back to the client the file in little chunks?

    Need programmers to hire?
    www.labcsp.com
    www.denisgottardello.it
    GMT+1
    Skype: mrdebug

    M 1 Reply Last reply 12 Apr 2025, 06:21
    0
    • M mrdebug
      11 Apr 2025, 21:51

      My QHttpServer server has to transfer a large file.
      With a little file I can use the write() function of QHttpServerResponder object like this

      responder.write(QBABuffer, "application/octet-stream");
      

      where QBABuffer is the intere file.
      But with a large file, how can I send back to the client the file in little chunks?

      M Offline
      M Offline
      mrdebug
      wrote on 12 Apr 2025, 06:21 last edited by
      #2

      This code seems to work but there is a problem

              if (File.open(QIODevice::ReadOnly)) {
                  responder.writeBeginChunked("application/octet-stream");
                  while (!File.atEnd()) {
                      QByteArray QBABuffer= File.read(1024);
                      if (QBABuffer.length()) {
                          if (File.atEnd()) responder.writeEndChunked(QBABuffer);
                          else responder.writeChunk(QBABuffer);
                      }
                  }
                  File.close();
              }
      

      The responder.writeChunk(QBABuffer); function is not blocking so the file will be load totally in ram before to send to the client.
      Is there a way to flush the socket?

      Need programmers to hire?
      www.labcsp.com
      www.denisgottardello.it
      GMT+1
      Skype: mrdebug

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Kevin Hoang
        wrote on 13 Apr 2025, 03:46 last edited by
        #3

        If you want to keep your current logic, you’ll need to add a blocking call like responder.socket()->waitForBytesWritten(-1); right after writeChunk.
        However, I think that's not a very good idea.

        A better way would be to open the file and use the QTcpSocket::bytesWritten event to send data chunk by chunk to the client. It would look something like this:

        QTcpSocket *socket = responder.socket();
        QObject::connect(socket, &QTcpSocket::bytesWritten, socket, [socket, file, responder = std::move(responder)]() mutable {
            if (file->atEnd()) {
                responder.done();
                file->deleteLater();
                return;
            }
        
            QByteArray chunk = file->read(1024);
            socket->write(chunk);
        });
        
        // Send the first chunk right away
        QByteArray chunk = file->read(1024);
        socket->write(chunk);
        

        This way, you avoid blocking the event loop and can handle multiple clients much more efficiently.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrdebug
          wrote on 13 Apr 2025, 06:56 last edited by
          #4

          Hi, thank you for the suggestion but I haven't got socket() function in QHttpServerResponder object.
          Which Qt version are you using?

          Need programmers to hire?
          www.labcsp.com
          www.denisgottardello.it
          GMT+1
          Skype: mrdebug

          1 Reply Last reply
          1
          • K Offline
            K Offline
            Kevin Hoang
            wrote on 13 Apr 2025, 10:16 last edited by
            #5

            What I shared was just an idea based on building a custom HTTP server.

            From what I see, Qt currently only offers a very basic HTTP server. If you want better performance, it’s probably best to look at Qt’s source code and create your own server based on it. I haven’t had a chance to test if writeChunk in Qt 6.8 really works as expected, but I have a feeling it might not.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrdebug
              wrote on 15 Apr 2025, 21:28 last edited by
              #6

              Solved using QtWebApp
              https://github.com/fffaraz/QtWebApp
              instead of QtHTTPServer

              Need programmers to hire?
              www.labcsp.com
              www.denisgottardello.it
              GMT+1
              Skype: mrdebug

              1 Reply Last reply
              0
              • K Offline
                K Offline
                Kevin Hoang
                wrote on 16 Apr 2025, 03:01 last edited by
                #7

                Congratulations! Glad to hear you got it resolved. I noticed that QtHttp uses socket->waitForBytesWritten(-1); when writing chunks — it’s a simple and straightforward approach. I’m still curious why QHttp doesn’t apply the same method.

                1 Reply Last reply
                0

                1/7

                11 Apr 2025, 21:51

                • Login

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