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.
-
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.
@CharlieG
to add up to @p3c0:
with aHEADrequest the webserver only returns the headers it would send when usingGETbut without it's content.
But note that theContent-Lengthheader isn't always present. For example when the server sends the data in chunked encoding. (But then this would be reflected in theTransfer-Encodingheader 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
-
@CharlieG Yes you are right. I completely overlooked the specific FTP requirement.
Unforutnately sendCustomRequest works only for HTTP/S protocol only or else sendingSIZEcommand would have been possible.
May be it could be possible to useQTcpSocketand connect to FTP server and issueSIZE. -
@CharlieG Yes you are right. I completely overlooked the specific FTP requirement.
Unforutnately sendCustomRequest works only for HTTP/S protocol only or else sendingSIZEcommand would have been possible.
May be it could be possible to useQTcpSocketand connect to FTP server and issueSIZE.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 ? -
In fact, I'll try ... but I think it will be the same problem with my current solution:
No. Using
QTcpSocketyou will have to do whatQNetworkAccessManagerdoes 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_commandsObviously 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.
-
In fact, I'll try ... but I think it will be the same problem with my current solution:
No. Using
QTcpSocketyou will have to do whatQNetworkAccessManagerdoes 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_commandsObviously 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 :( :( :( :(
-
Content-Length header isn't always present.
Read there http://doc.qt.io/qt-5/qnetworkaccessmanager.html[Edit aha_1980: Unrelated Link removed]
-
@Tusovshik Alright. Whats with the other link ? I find it not related to the question.
-
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()); -
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());@BFG9k
You code simulates a FTP request, which is by far not fail safe either and might only work in special cases. -
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());@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 ?