Results 1 to 12 of 12

Thread: Upload pdf file in the MySQL database

  1. #1
    Join Date
    Feb 2009
    Posts
    189
    Thanks
    2

    Smile 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

  2. #2
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Upload pdf file in the MySQL database

    Quote Originally Posted by boudie View Post
    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.
    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.

  4. #4
    Join Date
    Feb 2009
    Posts
    189
    Thanks
    2

    Default Re: Upload pdf file in the MySQL database

    where can i find the poppler library do i need to install it.

  5. #5
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Upload pdf file in the MySQL database

    Forget poppler (for now...)

    As ChrisW67 suggests:
    ...You could also extract the BLOB to a temporary file and use QDesktopServices:penUrl() to launch the user's PDF viewer of choice.
    It's much simpler to implement.

  6. #6
    Join Date
    Feb 2009
    Posts
    189
    Thanks
    2

    Default 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 .

    Qt Code:
    1. if(!ui.lineEdit_APPLICATION->text().isEmpty()) {
    2. QByteArray inside;
    3. QString filename = ui.lineEdit_APPLICATION->text();
    4. QStringList list = filename.split(QRegExp("/"), QString::SkipEmptyParts);
    5. QFile file(filename);
    6. if (file.exists()) {
    7. if (file.open(QFile::ReadOnly)) {
    8. inside = file.readAll();
    9. file.close();
    10. }
    11. }
    12. query4.bindValue(":applicationpdf",list.last());
    13. query4.bindValue(":resume",inside);
    14. list.clear();
    15. }
    16. else {
    17. query4.bindValue(":applicationpdf", "");
    18. query4.bindValue(":resume", "");
    19. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 10th October 2011 at 21:01. Reason: missing [code] tags

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Upload pdf file in the MySQL database

    It pays off to execute the query once it is prepared (if at all).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Feb 2009
    Posts
    189
    Thanks
    2

    Default 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

    Qt Code:
    1. void CDisplayEntry::openPDF()
    2. {
    3.  
    4. QSqlQuery query(console->mainwindow->db);
    5. QString queryString = "SELECT * FROM Tbl_StudentLink";
    6. query.exec(queryString);
    7.  
    8. int field1 = query.record().indexOf("resume");
    9. if(query.seek(index,false))
    10. {
    11. QUrl url = QUrl::fromEncoded(query.value(field1).toByteArray());
    12. QTextEdit *viewPDF = new QTextEdit(url.toString());
    13. viewPDF->show();
    14. }
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 11th October 2011 at 07:54. Reason: missing [code] tags

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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.

  10. #10
    Join Date
    Feb 2009
    Posts
    189
    Thanks
    2

    Default 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
    Qt Code:
    1. QByteArray inside="";
    2. QFile file(filename);
    3. if (file.exists()) {
    4. if (file.open(QFile::ReadOnly)) {
    5. inside = file.readAll();
    6. file.close();
    7. }
    8. query4.bindValue(":resume",inside);
    To copy to clipboard, switch view to plain text mode 

    Retrieving from the database and viewing in TextEdit
    Qt Code:
    1. QSqlQuery query(console->mainwindow->db);
    2. QString queryString = "SELECT * FROM Tbl_StudentLink";
    3. query.exec(queryString);
    4.  
    5. int field1 = query.record().indexOf("resume");
    6. if(query.seek(index,false))
    7. {
    8. // QUrl url = QUrl::fromEncoded(query.value(field1).toByteArray());
    9. QByteArray pdf = query.value(field1).toByteArray();
    10. QTextEdit *viewPDF = new QTextEdit(QString(pdf));
    11. viewPDF->show();
    12. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Upload pdf file in the MySQL database

    Qt Code:
    1. QByteArray pdf = query.value(field1).toByteArray();
    2. QTextEdit *viewPDF = new QTextEdit(QString(pdf));
    To copy to clipboard, switch view to plain text mode 
    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 ?

  12. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Upload pdf file in the MySQL database

    Quote Originally Posted by sujan.dasmahapatra View Post
    PDF is not working only txt files are working.
    Surprising given the mangling you put the content rhough in your code snippets.
    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.
    Give me some suggestion how can i view pdf document.
    We already have, several times. Here for example.

    check my snippet.

    Retrieving from the database and viewing in TextEdit
    Qt Code:
    1. QSqlQuery query(console->mainwindow->db);
    2. QString queryString = "SELECT * FROM Tbl_StudentLink";
    3. query.exec(queryString);
    4.  
    5. int field1 = query.record().indexOf("resume");
    6. if(query.seek(index,false))
    7. {
    8. // QUrl url = QUrl::fromEncoded(query.value(field1).toByteArray());
    9. QByteArray pdf = query.value(field1).toByteArray();
    10. QTextEdit *viewPDF = new QTextEdit(QString(pdf));
    11. viewPDF->show();
    12. }
    To copy to clipboard, switch view to plain text mode 
    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.

Similar Threads

  1. how to save a image file ( say png ) into mysql database?
    By AviMittal in forum Qt Programming
    Replies: 12
    Last Post: 21st July 2011, 12:49
  2. Problem in Http file upload
    By dipeshtech in forum Qt Programming
    Replies: 1
    Last Post: 28th April 2011, 08:44
  3. Replies: 1
    Last Post: 21st October 2010, 04:59
  4. Qhttp Upload file
    By danny.lesnik in forum Qt Programming
    Replies: 5
    Last Post: 11th December 2009, 09:02
  5. QNetworkRequest file upload -- please help
    By Runtime Technologies in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2009, 15:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.