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. QNetworkAccessManager : Get file size before download
Forum Updated to NodeBB v4.3 + New Features

QNetworkAccessManager : Get file size before download

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 6 Posters 7.3k Views 2 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.
  • CharlieGC Offline
    CharlieGC Offline
    CharlieG
    wrote on last edited by
    #1

    Hi,

    I work on a (very) little ftp client (only download files with ftp). It well works.

    I will wish get the file size before download. I trie with downloadProgress and readBufferSize (QNetworkReply) but I can't get this size.

    Have you an idea ?

    Thank you in advance.

    raven-worxR 1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by p3c0
      #2

      @CharlieG Use head. When finished look for QNetworkRequest::ContentLengthHeader in QNetworkReply using header.
      And remember to check for redirects or else you wont get the size.

      157

      1 Reply Last reply
      5
      • CharlieGC CharlieG

        Hi,

        I work on a (very) little ftp client (only download files with ftp). It well works.

        I will wish get the file size before download. I trie with downloadProgress and readBufferSize (QNetworkReply) but I can't get this size.

        Have you an idea ?

        Thank you in advance.

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        @CharlieG
        to add up to @p3c0:
        with a HEAD request the webserver only returns the headers it would send when using GET but without it's content.
        But note that the Content-Length header isn't always present. For example when the server sends the data in chunked encoding. (But then this would be reflected in the Transfer-Encoding header returned by the server)
        So there is no guaranteed way to determine the download size before downloading, and in the mentioned chunked-encoding you even do know the exact size only after the download has finished.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        5
        • CharlieGC Offline
          CharlieGC Offline
          CharlieG
          wrote on last edited by
          #4

          Hi @raven-worx,
          Hi @p3c0,

          Thank you for your help.
          Unfortunately I feel that head don't support the FTP protocol.
          Anyway when I do:

          m_reply = manager-> head (QNetworkRequest (urlFile));
          

          I have a message saying that the ftp protocol is unknown.

          As for now I plan to use an XML file to list files to download (I have not found how to do it with QNetworkAccessManager) I will surely add the approximate size ... shame

          1 Reply Last reply
          1
          • p3c0P Offline
            p3c0P Offline
            p3c0
            Moderators
            wrote on last edited by
            #5

            @CharlieG Yes you are right. I completely overlooked the specific FTP requirement.
            Unforutnately sendCustomRequest works only for HTTP/S protocol only or else sending SIZE command would have been possible.
            May be it could be possible to use QTcpSocket and connect to FTP server and issue SIZE.

            157

            CharlieGC 1 Reply Last reply
            2
            • p3c0P p3c0

              @CharlieG Yes you are right. I completely overlooked the specific FTP requirement.
              Unforutnately sendCustomRequest works only for HTTP/S protocol only or else sending SIZE command would have been possible.
              May be it could be possible to use QTcpSocket and connect to FTP server and issue SIZE.

              CharlieGC Offline
              CharlieGC Offline
              CharlieG
              wrote on last edited by
              #6

              Hello @p3c0

              In fact, I'll try ... but I think it will be the same problem with my current solution:

              ba QByteArray m_reply- => readAll ();
              m_size ba.size + = ();
              

              With these solutions, I must use two manager.get (QNetworRequest)

              • the first to have the file size;
              • the second to download.

              So I think I will double the time for action.
              What do you think ?

              1 Reply Last reply
              0
              • p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #7

                @CharlieG

                In fact, I'll try ... but I think it will be the same problem with my current solution:

                No. Using QTcpSocket you will have to do what QNetworkAccessManager does in the background i.e send commands by yourself. Using sockets you will have to write data manually over that connection. Here is a list of FTP commands which you will require:
                https://en.wikipedia.org/wiki/List_of_FTP_commands

                Obviously its a tedious and time consuming task.

                Now to your implementation, it seems that you are first trying to download the file in parts and calculating its size.

                So I think I will double the time for action.

                Why do you do it twice ? You already have the data while calculating the size as you download the parts.

                157

                CharlieGC 1 Reply Last reply
                3
                • p3c0P p3c0

                  @CharlieG

                  In fact, I'll try ... but I think it will be the same problem with my current solution:

                  No. Using QTcpSocket you will have to do what QNetworkAccessManager does in the background i.e send commands by yourself. Using sockets you will have to write data manually over that connection. Here is a list of FTP commands which you will require:
                  https://en.wikipedia.org/wiki/List_of_FTP_commands

                  Obviously its a tedious and time consuming task.

                  Now to your implementation, it seems that you are first trying to download the file in parts and calculating its size.

                  So I think I will double the time for action.

                  Why do you do it twice ? You already have the data while calculating the size as you download the parts.

                  CharlieGC Offline
                  CharlieGC Offline
                  CharlieG
                  wrote on last edited by CharlieG
                  #8

                  @p3c0
                  Thank you for FTP commands... I'll see it tonight.

                  @p3c0 said :

                  Why do you do it twice ? You already have the data while calculating the size as you download the parts.

                  Because I want to tell the user the size of files that will download before it does.
                  I can indeed use once the get () method, but the problem is the same. I must "readAll" to know the file size.
                  If the user doesn't want to download, it will still be used "data" (it's a mobile appliction !!!!)

                  [EDIT]
                  I tried using the HTTP address of a file I want to use, but only headers are available :

                  ("Date", "Server", "Last-Modified", "ETag", "Accept-Ranges", "Cache-Control", "Expires", "Vary", "Content-Encoding", "Keep-Alive", "Connection", "Transfer-Encoding", "Content-Type")
                  

                  Not size ..... I'm tired... :(

                  And I have not worked on the recovery of files into a directory in FTP ...

                  I'm very tired :( :( :( :(

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    Tusovshik
                    wrote on last edited by aha_1980
                    #9

                    Content-Length header isn't always present.
                    Read there http://doc.qt.io/qt-5/qnetworkaccessmanager.html

                    [Edit aha_1980: Unrelated Link removed]

                    1 Reply Last reply
                    0
                    • p3c0P Offline
                      p3c0P Offline
                      p3c0
                      Moderators
                      wrote on last edited by
                      #10

                      @Tusovshik Alright. Whats with the other link ? I find it not related to the question.

                      157

                      1 Reply Last reply
                      1
                      • B Offline
                        B Offline
                        BFG9k
                        wrote on last edited by
                        #11
                        QTcpSocket sock;
                        sock.connectToHost("test.rebex.net", 21);
                        sock.waitForConnected();
                        
                        sock.write("USER demo\r\n");
                        sock.waitForBytesWritten();
                        sock.waitForReadyRead();
                        QString s = QString(sock.readAll());
                        
                        sock.write("PASS password\r\n");
                        sock.waitForBytesWritten();
                        sock.waitForReadyRead();
                        s = QString(sock.readAll());
                        
                        sock.write("SIZE /pub/example/pocketftp.png\r\n");
                        sock.waitForBytesWritten();
                        sock.waitForReadyRead();
                        s = QString(sock.readAll());
                        
                        raven-worxR JonBJ 2 Replies Last reply
                        0
                        • B BFG9k
                          QTcpSocket sock;
                          sock.connectToHost("test.rebex.net", 21);
                          sock.waitForConnected();
                          
                          sock.write("USER demo\r\n");
                          sock.waitForBytesWritten();
                          sock.waitForReadyRead();
                          QString s = QString(sock.readAll());
                          
                          sock.write("PASS password\r\n");
                          sock.waitForBytesWritten();
                          sock.waitForReadyRead();
                          s = QString(sock.readAll());
                          
                          sock.write("SIZE /pub/example/pocketftp.png\r\n");
                          sock.waitForBytesWritten();
                          sock.waitForReadyRead();
                          s = QString(sock.readAll());
                          
                          raven-worxR Offline
                          raven-worxR Offline
                          raven-worx
                          Moderators
                          wrote on last edited by raven-worx
                          #12

                          @BFG9k
                          You code simulates a FTP request, which is by far not fail safe either and might only work in special cases.

                          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                          If you have a question please use the forum so others can benefit from the solution in the future

                          1 Reply Last reply
                          0
                          • B BFG9k
                            QTcpSocket sock;
                            sock.connectToHost("test.rebex.net", 21);
                            sock.waitForConnected();
                            
                            sock.write("USER demo\r\n");
                            sock.waitForBytesWritten();
                            sock.waitForReadyRead();
                            QString s = QString(sock.readAll());
                            
                            sock.write("PASS password\r\n");
                            sock.waitForBytesWritten();
                            sock.waitForReadyRead();
                            s = QString(sock.readAll());
                            
                            sock.write("SIZE /pub/example/pocketftp.png\r\n");
                            sock.waitForBytesWritten();
                            sock.waitForReadyRead();
                            s = QString(sock.readAll());
                            
                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #13

                            @BFG9k
                            If you are wishing to use FTP, have you at least considered QFtp: https://forum.qt.io/topic/64412/how-to-add-qftp-in-qt5, https://forum.qt.io/topic/86168/qftp-under-qt-5 ?

                            1 Reply Last reply
                            0

                            • Login

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