Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 18 Next »


Storing and Displaying Files in a Database

Ignition can store different types of files into a database by storing the raw file bytes into a special database column. Ignition can also pull these file bytes out and display certain files within a Client.

Each database has different column types that are used to store files, so it is important to check with your database documentation to see which data type the column would need to be set to for it to accept file bytes.  For example, in MySQL, the datatype that accepts PDF bytes is a LongBlob datatype, so you will need to set the PDF Data column to the LongBlob datatype. MS SQL accepts the Varbinary datatype, so you'll need to set the PDF Data column to a Varbinary datatype.

Example - PDF File

One of the most common file types that is stored is PDF files. This allows you to store each PDF file within the central database where each client will have access to it, instead of placing the file in a shared drive that all Client computers have access to.

On this page ...


IULocgo


Storing Files in a Database

Uploading PDF Files to the Database

Ignition can upload PDF documents to a database. Imagine the user pressing a button, a file browser pops up, the user selects the PDF file and presses Enter, and then the file gets uploaded into the database. The following script demonstrates how this might work.


Python - Uploads PDF Files to a Database Using a Button
# Find the path to the PDF file.
path = system.file.openFile("pdf")

# Read the file as bytes, and ask the user for a filename.
data = system.file.readFileAsBytes(path)
name = system.gui.inputBox("Enter a name for the file")

# Insert the data and name into the database.
system.db.runPrepUpdate("INSERT INTO documents (name, data) VALUES (?,?)", [name,data])

Displaying PDF Files from the Database

Ignition can render the PDF docment inside the PDF Viewer component, which is a part of the Reporting Module. To view PDF files in the Client, your Ignition server must have the Reporting Module installed. 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 Dropdown component property event handler.

Python - Displays PDF Files from a Database Using the PDF Viewer
# Get the id from the dropdown.
id = event.newValue

# Query for the raw pdf data and name by using the id.
bytes = system.db.runScalarQuery("SELECT data FROM documents WHERE id = %d" % id)
name = system.db.runScalarQuery("SELECT name FROM documents WHERE id = %d" % id)

# Load the bytes and the name into the PDF Viewer component.
event.source.parent.getComponent('PDF Viewer').loadPDFBytes(bytes, name)



Related Topics ...

 

 

  • No labels