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


Storing Files in a Database

Uploading PDF Files to the Database

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 function will not work using default Client Permission settings. The Legacy Database Access will need to be enabled for this to work.

  1. Add a Button component to a new Main Window.
  2. Change the Text property to say Add File.
  3. Right click on the button and select Scripting.
    1. 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 then insert that into the database, along with a user selected file name.

      Python - Uploads PDF Files to a Database Using a Button
      # 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])
  4. Test out the script by putting the designer into Preview Mode and clicking the button.
    1. First select the file to load.
    2. Then enter in a file name.

Displaying PDF Files from the Database

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