QNetworkAccessManager : Get file size before download



  • 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.


  • Moderators

    @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.


  • Moderators

    @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.



  • 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


  • Moderators

    @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.



  • 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 ?


  • Moderators

    @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.



  • @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 :( :( :( :(




  • Moderators

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


Log in to reply