Results 1 to 4 of 4

Thread: Streaming files to web browser using QTcpSocket

  1. #1
    Join Date
    Mar 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows
    Wiki edits
    2

    Lightbulb Streaming files to web browser using QTcpSocket

    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

  2. #2
    Join Date
    May 2010
    Posts
    1
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Streaming files to web browser using QTcpSocket

    Thanks A LOT! I subscribed to this forum just to say Thank you, and it seems that I'll hang out here more often. Again thanks

  3. #3
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    1
    Qt products
    Qt4

    Default Re: Streaming files to web browser using QTcpSocket

    thanks a lot this is a g8t help but i want to know how do u stream an audio file from the web browser...please help....
    thank u in advance

  4. #4
    Join Date
    Jul 2014
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Android

    Default Re: Streaming files to web browser using QTcpSocket

    Thank you !! You have saved my day

Similar Threads

  1. Compress and Uncompress large files in QTcpSocket
    By vishesh in forum Qt Programming
    Replies: 4
    Last Post: 26th June 2007, 23:21
  2. Replies: 11
    Last Post: 4th July 2006, 16:09

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.