PDA

View Full Version : How to download using QHttp?



Gokulnathvc
23rd May 2011, 11:02
How to download using QHttp? But file should not be saved in the local hard disk, It has to save in some memory variable, How to to this?

ChrisW67
23rd May 2011, 11:08
Don't use QHttp in new code. To quote the docs:


This class is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.


You use QNetworkAccessManager and QNetworkReply::read() or readAll() the data into a QByteArray

Gokulnathvc
23rd May 2011, 11:13
I have used QHttp for downloading it, now the file is being downloaded , and saved in the local disk, But i just want to read that file, storing it in a variable, and not saving it as a file in local hard disk. How to perform this with QHttp??

squidge
23rd May 2011, 13:11
I'd use readAll rather than specifying the i/o device in the request.

But as ChrisW67 says, you should be using QNetworkAccessManager, as QHttp is deprecated.

Berryblue031
23rd May 2011, 14:37
Check out the "HTTP Client" example.

on the QNetworkReply::finished signal call reply->readAll() if the data is human readable you can do something like


QString replyString;
if (reply->isReadable())
{
replyString = QString::fromUtf8(reply->readAll().data());
}

ChrisW67
23rd May 2011, 23:33
I have used QHttp for downloading it, now the file is being downloaded , and saved in the local disk, But i just want to read that file, storing it in a variable, and not saving it as a file in local hard disk. How to perform this with QHttp??

Even if you persist in using the obsolete class you can use QHttp::readAll() to put the data into a QByteArray (either bit-by-bit using in response to readyRead(), or all at once in response to finished()). That is, use exactly the same method as used in the HTTP Example. This information really easy to find if you read the docs and work through the examples (until you understand them).

Gokulnathvc
25th May 2011, 05:29
How to read the whole file at once using readAll()?

ChrisW67
25th May 2011, 06:11
Wait until the QNetworkReply::finished() or QNetworkAccessManager::finished() signal is emitted and then call readAll(). If you are still using QHttp then you can work out the equivalent for yourself.

If your file is large, or you don't know its size, then trying to hold the whole thing in memory might be a mistake.