QHttpServer and large file
-
wrote 22 days ago last edited by
My QHttpServer server has to transfer a large file.
With a little file I can use the write() function of QHttpServerResponder object like thisresponder.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? -
My QHttpServer server has to transfer a large file.
With a little file I can use the write() function of QHttpServerResponder object like thisresponder.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?wrote 21 days ago last edited byThis 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? -
wrote 20 days ago last edited by
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.
-
wrote 20 days ago last edited by
Hi, thank you for the suggestion but I haven't got socket() function in QHttpServerResponder object.
Which Qt version are you using? -
wrote 20 days ago last edited by
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.
-
wrote 18 days ago last edited by
Solved using QtWebApp
https://github.com/fffaraz/QtWebApp
instead of QtHTTPServer -
wrote 17 days ago last edited by
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.
7/7