Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
JeffFrancom
Posts: 15
Registered: ‎12-20-2012
My Device: BlackBerry 10 Dev Alpha
Accepted Solution

Downloading and launching pdf

Hey guys,

 

So I have encountered a situation where I need to download and launch a pdf in my app. I have already implemented launching the pdf using an Adobe invoke (and it works fine) but every time it goes to open the pdf I get a "The document cannot be opened because it is corrupted or damaged" error. So it appears that my process for downloading and saving the pdf is erroneous. 

 

To download the pdf I simply make a request to the url, and save the data returned to a file. Clearly I do not have a proper grasp on what I need to do to download/save a pdf. Here is how I am currently saving the pdf:

 

QByteArray pdfData = reply->readAll();

		QString url = reply->url().toString();

		QStringList urlSplit = url.split(".");
		QString fileType = urlSplit.at(urlSplit.length()-1);

		QByteArray bytes;
		bytes.append(url);

		QString fileName = QString(QCryptographicHash::hash(bytes, QCryptographicHash::Md5).toHex()) + "." + fileType;

		QString fileTargetUrl = QDir::currentPath() + "/data/downloads/" + fileName;

		QFile textFile(fileTargetUrl);
		textFile.open(QIODevice::WriteOnly | QIODevice::Unbuffered);

		QTextStream textOut(&textFile);

		textOut << pdfData;

		textOut.flush();
		textFile.close();

 Is there something wrong with how I am approaching this? If so, what can I change to properly download the pdf?

 

Thanks for any help possible,

- Jeff

Developer
JeffFrancom
Posts: 15
Registered: ‎12-20-2012
My Device: BlackBerry 10 Dev Alpha

Re: Downloading and launching pdf

Ok, so I found the problem. Converting the reply->readAll() to a QByteArray first was causing an error with the pdf's data (corruption of the pdf's encoding). If the reply is directly inserted into the file the data does not get corrupted.

 

Here is the functioning code:

 

QString url = reply->url().toString();

QStringList urlSplit = url.split(".");
QString fileType = urlSplit.at(urlSplit.length()-1);

QByteArray bytes;
bytes.append(url);

QString fileName = QString(QCryptographicHash::hash(bytes, QCryptographicHash::Md5).toHex()) + "." + fileType;

QString fileTargetUrl = QDir::currentPath() + "/data/downloads/" + fileName;

QFile textFile(fileTargetUrl);
textFile.open(QIODevice::WriteOnly | QIODevice::Unbuffered);

textFile.write(reply->readAll());
textFile.flush();
textFile.close();

 - Jeff