Upload pdf file in the MySQL database
Dear Friends
I am working with MySQL database using Qt. I want to store a pdf file in the database. and show the contents of the pdf when user wants. How can I upload a pdf file in the database and open the file when needed from the database. Please give me some idea.
I know only two types INT and VARCHAR to store int and some string. But how can i upload the pdf file. Please help me if you know anyone. Thanks a lot. sujan
Re: Upload pdf file in the MySQL database
You can store it in the database as a BLOB (Binary Large Object).
To show the pdf you can use the poppler library.
Re: Upload pdf file in the MySQL database
Quote:
Originally Posted by
boudie
You can store it in the database as a BLOB (Binary Large Object).
This is handled as a QByteArray inside your Qt code. Reading a PDF file into a QByteArray is trivial. You will need to use a QSqlQuery::prepare()-ed query and QSqlQuery::bindValue() to insert it into the database.
Quote:
To show the pdf you can use the poppler library.
Qt does not have any built-in PDF viewing capability: Poppler is an external library that could be use to add a in-program viewer. You could also extract the BLOB to a temporary file and use QDesktopServices::openUrl() to launch the user's PDF viewer of choice.
Re: Upload pdf file in the MySQL database
where can i find the poppler library do i need to install it.
Re: Upload pdf file in the MySQL database
Forget poppler (for now...)
As ChrisW67 suggests:
Quote:
...You could also extract the BLOB to a temporary file and use QDesktopServices::openUrl() to launch the user's PDF viewer of choice.
It's much simpler to implement.
Re: Upload pdf file in the MySQL database
Please check this code snippet ui.lineEdit_APPLICATION is the control fetching the filename..........With this my table is not getting updated with the value resume........My table is blank........Whats wrong please tell me .
Code:
if(!ui.lineEdit_APPLICATION->text().isEmpty()) {
QString filename
= ui.
lineEdit_APPLICATION->text
();
if (file.exists()) {
if (file.
open(QFile::ReadOnly)) { inside = file.readAll();
file.close();
}
}
query4.bindValue(":applicationpdf",list.last());
query4.bindValue(":resume",inside);
list.clear();
}
else {
query4.bindValue(":applicationpdf", "");
query4.bindValue(":resume", "");
}
Re: Upload pdf file in the MySQL database
It pays off to execute the query once it is prepared (if at all).
Re: Upload pdf file in the MySQL database
If I load a pdf or .doc file my table is not updated. But if I load a jpeg file then its uploading in the database. Please tell me why its happening. why pdf or doc file data is not uploading as QByteArray. Thanks Sujan
Added after 24 minutes:
If I give LONGBLOB instead of BLOB then its storing something. Can u tell me how can I fetch the data from database check if this is correct
Code:
void CDisplayEntry::openPDF()
{
QString queryString
= "SELECT * FROM Tbl_StudentLink";
query.exec(queryString);
int field1 = query.record().indexOf("resume");
if(query.seek(index,false))
{
QUrl url
= QUrl::fromEncoded(query.
value(field1
).
toByteArray());
viewPDF->show();
}
}
Re: Upload pdf file in the MySQL database
Here's what you are doing:
- You store the actual bytes of the PDF file into the database
- You retrieve the actual bytes of the PDF file and try to interpret those bytes as a URL
- You use a QTextEdit to display a string made from the URL and expect to see the PDF file.
Does this sound correct to you?
Go back and read the earlier responses about your options.
Re: Upload pdf file in the MySQL database
PDF is not working only txt files are working. Poppler library is linux based I need windows. Give me some suggestion how can i view pdf document.
check my snippet.
// To read the document and store in the database
Code:
if (file.exists()) {
if (file.
open(QFile::ReadOnly)) { inside = file.readAll();
file.close();
}
query4.bindValue(":resume",inside);
Retrieving from the database and viewing in TextEdit
Code:
QString queryString
= "SELECT * FROM Tbl_StudentLink";
query.exec(queryString);
int field1 = query.record().indexOf("resume");
if(query.seek(index,false))
{
// QUrl url = QUrl::fromEncoded(query.value(field1).toByteArray());
QByteArray pdf
= query.
value(field1
).
toByteArray();
viewPDF->show();
}
Re: Upload pdf file in the MySQL database
Code:
QByteArray pdf
= query.
value(field1
).
toByteArray();
What do you expect to see in text edit when you convert the bytes of the file to a string ? A rendered pdf document ?
Anyway, why don't you just write the "pdf" byte array to a file and open it with the system default pdf viewer ?
Re: Upload pdf file in the MySQL database
Quote:
Originally Posted by
sujan.dasmahapatra
PDF is not working only txt files are working.
Surprising given the mangling you put the content rhough in your code snippets.
Quote:
Poppler library is linux based I need windows.
Poppler can be built in Windows but it is an effort. From the discussion to date I suspect that you do not have the requisite understanding to build it. For example: Compiling Poppler on Windows. MuPDF is another option under GPL3.
Quote:
Give me some suggestion how can i view pdf document.
We already have, several times. Here for example.
Quote:
check my snippet.
Retrieving from the database and viewing in TextEdit
Code:
QString queryString
= "SELECT * FROM Tbl_StudentLink";
query.exec(queryString);
int field1 = query.record().indexOf("resume");
if(query.seek(index,false))
{
// QUrl url = QUrl::fromEncoded(query.value(field1).toByteArray());
QByteArray pdf
= query.
value(field1
).
toByteArray();
viewPDF->show();
}
Uh huh! You have tweaked this since the last time but you still have not grasped that a PDF in a binary data stream and that displaying the raw bytes in a text edit is wasting your time. On the upside you are no longer trying to mangle the binary data by treating it as a URL.
Write the data to a temporary file (use QDesktopServices to find where this should go), build a URL pointing at the temporary file, and use openUrl() to launch the user's PDF reader. This is about as simple as it gets.