02-26-2013 10:20 AM
Hi, I'm has API from webservice that will download a zip file. (If iI try hit this API on browser, zip file will be downloaded)
How to download it and how to open / extract it on blackberry cascades?
I'm trying to write it to file from
reply->readAll() on HttpFinished
but not success to write / download it
Please help
Thanks
Solved! Go to Solution.
02-26-2013 11:01 PM
Hi, any suggestion?
Thanks
02-27-2013 04:50 AM
Hi,
you have more ways to download a file, I will show one of them.
I'm using QHttp to download large files against QNetworkRequest. The API reference tells, that QHttp is old, and if we can, try to avoid it. But QNetworkRequest has no methods to download a remote file directly into a local file. So my method is old, but it works.
You need a QFile* and a QHttp* in the header:
private:
QFile* zipFile;
QHttp* zipRequest;
///cpp
void MyApp::getZip()
{
QUrl* zipUrl = new QUrl("http://www.xxx.com/example.zip");
zipRequest = new QHttp(this);
zipRequest->setHost(zipUrl->host());
zipFile = new QFile("path + example.zip");
if (!file->open(QIODevice::WriteOnly) )
{
//can't open the file for write
} else {
//file created and opened
zipRequest->get(zipUrl->path(), zipFile);
connect(zipRequest, SIGNAL(done(bool)), this, SLOT(done(bool)));
}
}
void MyApp::done(bool error)
{
zipRequest->deleteLater();
zipFile->close();
if (error)
qDebug() << zipRequest->errorString();
}That's all.
To extract a zip file I'm using QuaZIP.
Here is a link:
http://quazip.sourceforge.net/
Hope it helps,
cheers,
chriske
02-27-2013 04:54 AM
Hi chriske,
I will try your suggestion.
Thanks for your respond.
02-27-2013 05:25 AM
Your welcome.
I've forgot, but there are other signals to monitor the download process, for example
void dataReadProgress(int done,int total);
You can find them in the API reference of QHttp.
03-03-2013 11:45 PM
I'm using this, and it work
void MyClass::downloadZip(const QString &aUrl)
{
QUrl *zipUrl = new QUrl(aUrl);
zipRequest = new QHttp(this);
zipRequest->setHost(zipUrl->host());
zipRequest->get(aUrl);
connect(zipRequest, SIGNAL(requestFinished(int, bool)), this, SLOT(zipFinished(int, bool)));
}
void MyClass::zipFinished(int aId, bool aError)
{
if (aError)
{
qDebug() << zipRequest->errorString();
showToast("Download fail");
}
else
{
QFile *zipFile = new QFile(QDir::currentPath() + "/app/native/assets/myZip.zip");
if (zipFile->open(QIODevice::WriteOnly))
{
zipFile->write(zipRequest->readAll());
zipFile->flush();
zipFile->close();
}
delete zipFile;
}
}