Example - Storing Files in a Database
Ignition can display and store PDF files from data stored in databases.
Displaying PDF files from the database
Ignition can render the PDF inside the PDF viewer component. The PDF viewer component is a free module available for download from Inductive Automation's website. To view PDF files in the Client, your Ignition server must have this module. Once the module is installed, you can load the bytes from the database into the PDF viewer component with the following script which is enacted on a drop-down component property event handler.
id = event.newValue #First get the id from the dropdown.
bytes = system.db.runScalarQuery(
"SELECT data FROM documents WHERE id = %d"
% id) #Query
for
the raw pdf data by using the id.
name = system.db.runScalarQuery(
"SELECT name FROM documents WHERE id = %d"
% id) #Query
for
the file's name by using the id.
event.source.parent.getComponent(
'PDF Viewer'
).loadPDFBytes(bytes, name) #Load the bytes and the name into the PDF viewer component.
Uploading PDF files to the database
Ignition can upload PDF documents to a database. Imagine the user selecting a button, a file browser pops up, the user selects the PDF file, presses enter and the file gets uploaded into the database. The following script demonstrates how this might work:
path = system.file.openFile(
"pdf"
) #Find the path to the PDF file.
data = system.file.readFileAsBytes(path) #Read the file as bytes.
name = system.gui.inputBox(
"Enter a name for the file"
) #Have the user enter a name
for
the file.
system.db.runPrepUpdate(
"INSERT INTO documents (name, data) VALUES (?,?)"
, [name,data]) #Insert the data and name into the database.
In a database, each column is a certain datatype. The datatype where you can store PDF bytes is a LONGBLOB datatype, therefore to insert the PDF file's bytes to a column the column's datatype must be a LONGBLOB.