Hi,

i have to post this solution, because i spent 2 days with founding it.

If you are developing a http service (server), you may be looking for a way, how to send non-text files to browser.

Example: I develop a WMS Server, which sends in some concrete cases an XML file and in the others image with a map. But the question is, how to stream the file to server?!

Solution: You have opened QTcpSocket and now you have to send an image (or any other file) to a server.

Qt Code:
  1. QTextStream resultStream(mySocket); // QTcpSocket* mySocket
  2. resultStream.setAutoDetectUnicode(true); // if needed
  3.  
  4. // Header in each case
  5. resultStream << "HTTP/1.0 200 Ok\r\n";
  6. resultStream << "Content-Type: " + contentType + "\r\n"; // e.g. image/png
  7. resultStream << "\r\n"; // end of header
  8.  
  9. if (contentType == "text/xml") { // should be extended - text/plain, text/html, ...
  10. resultStream << content;// in this case QDomDocument converted to QString
  11. } else { // maps and other files
  12.  
  13. QFile transferedFile("file.png");
  14. if (!transferedFile.open(QIODevice::ReadOnly)) {
  15. // handle error
  16. }
  17.  
  18. resultStream.flush(); // flush buffer with header to the web browser
  19.  
  20. // Streaming the file
  21. QByteArray block = transferedFile.readAll();
  22. mySocket->write(block);
  23. mySocket->close(); // finished :)
  24. }
To copy to clipboard, switch view to plain text mode 
Hope it helped at least one person