Contents
Strategic Partner Links
Sepasoft - MES Modules
Cirrus Link - MQTT Modules
Resources
Knowledge Base Articles
Inductive University
Forum
IA Support
SDK Documentation
SDK Examples
With a simple script on a Button component, we can store a PDF file into the database so that any user can view it later. This part does not use a Named Query, because the Named Query Parameters do not have a data type that allows us to pass in the raw file bytes. We can instead use system.db.runPrepUpdate to call a query from the script.
This example requires that you have a table with a byte array column in it. For example: MySQL user the BLOB data type and MSSQL uses the varbinary() data type.
This function will not work using default Client Permission settings. The Legacy Database Access will need to be enabled for this to work.
Navigate to the Script Editor Tab of the actionPerformed Event Handler. Here we can put a script that will grab the file bytes using the file path and the system.file.readFileAsBytes function, and then insert that into the database, along with a user selected file name.
# Find the path to the PDF file. path = system.file.openFile("pdf") # Check to ensure that the user actually selected a filepath. if path != None: # Read the file as bytes. data = system.file.readFileAsBytes(path) # Ask the user to enter in a filename. # Will grab just the filename and extension from the path as a suggestion. name = system.gui.inputBox("Enter a name for the file", path.split('\\')[-1]) # Check to ensure that the user entered a name for the file. if name != None: # Insert the data and name into the database. system.db.runPrepUpdate("INSERT INTO files (fileName, fileBytes) VALUES (?,?)", [name,data])
Ignition can render a PDF document 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 no Parameters, add a query to select all the files in our files table.
SELECT id, fileName FROM files
Add the query to select the file name and bytes based on the selected ID.
SELECT fileName, fileBytes FROM files WHERE id = :fileID
This script will take any new selected value and use it in the Named Query we made in step 2 to get the file name and bytes. We can then load the bytes into the PDF Viewer.
# Check to see if the property that changed was the Selected Value property. if event.propertyName == "selectedValue": # Run the query to grab the file name and bytes using the new selected ID value. system.db.runNamedQuery("Read File", {"fileID":event.newValue}) # Grab the file bytes and name from the same row. bytes = data.getValueAt(0, "fileBytes") name = data.getValueAt(0, "fileName") # Load the bytes into the PDF Viewer component. event.source.parent.getComponent('PDF Viewer').loadPDFBytes(bytes, name)