Show file stored in Database

I’m having trouble getting a BLOB file from the database to show in the client.
Has anyone done this?
I’ve been able to write file to the database but not able to view them.

A little more information on what your trying to do will be very useful. What is the blob your saving? Text, image, etc.? What have you tried already? What database are you using? What version of Ignition…

I’m trying to view a PDF file. I would like to be able to view image and .doc as well.
I’m using MySQL and Ignition 7.5.

What I’m trying to accomplish is I need the ability to tie files to certain part numbers and once the operator recalls the part number from the database a table will display the available documentation, that goes along with it.

I’ve tied much other than system.file.writeFile example but I got this error

[code]Traceback (most recent call last):

File “event:actionPerformed”, line 6, in

java.lang.NullPointerException: java.lang.NullPointerException

caused by NullPointerException

Ignition v7.5.0 (b1079)
Java: Oracle Corporation 1.7.0_05
[/code]

Script

data = event.source.parent.getComponent('Text Field').text
filename = "temp.pdf"
if filename != None:
	system.file.writeFile(filename, data)

Could you attach the full stack trace please? When that error message pops up and you switch to the details tab there should be a link at the bottom that says “full”. Clicking that should display the entire message.

I’m not sure why this isn’t working for you. I have done something similar with images but I don’t think I’ve ever tried it with a pdf. Are you following an example that pulls the blob data from the database into a text box?

[code]Traceback (most recent call last):
File “event:actionPerformed”, line 6, in
at com.inductiveautomation.ignition.common.script.builtin.FileUtilities.writeFile(FileUtilities.java:122)

at com.inductiveautomation.ignition.common.script.builtin.FileUtilities.writeFile(FileUtilities.java:108)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

java.lang.NullPointerException: java.lang.NullPointerException

at org.python.core.Py.JavaError(Py.java:481)
at org.python.core.Py.JavaError(Py.java:474)
at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:188)
at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:204)
at org.python.core.PyObject.__call__(PyObject.java:404)
at org.python.core.PyObject.__call__(PyObject.java:408)
at org.python.pycode._pyx31.f$0(<event:actionPerformed>:6)
at org.python.pycode._pyx31.call_function(<event:actionPerformed>)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1261)
at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:539)
at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.runActions(ActionAdapter.java:155)
at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.invoke(ActionAdapter.java:266)
at com.inductiveautomation.factorypmi.application.binding.action.RelayInvocationHandler.invoke(RelayInvocationHandler.java:55)
at $Proxy15.actionPerformed(Unknown Source)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Caused by: java.lang.NullPointerException
at com.inductiveautomation.ignition.common.script.builtin.FileUtilities.writeFile(FileUtilities.java:122)
at com.inductiveautomation.ignition.common.script.builtin.FileUtilities.writeFile(FileUtilities.java:108)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.python.core.PyReflectedFunction.call(PyReflectedFunction.java:186)
… 49 more

Ignition v7.5.0 (b1079)
Java: Oracle Corporation 1.7.0_05
[/code]

I have the script tied to a button and upon clicking the button I thought it would create a temp file on the desktop. Maybe I’m not understanding the manuals explanation of how this is used.

You need to specify a drive letter. It doesn’t know where to save the file which is causing the NPE. Do the following:data = event.source.parent.getComponent('Text Field').text filename = "c:\\temp.pdf" if filename != None: system.file.writeFile(filename, data)

Travis, that did work and got the file to the temp folder, Thank you.

But when I went to open the .pdf file I got an error, “Adobe Reader could not open ‘temp.pdf’ because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn’t correctly decoded).”

I have Adobe Reader version 10.1.3

I also tried to view the file with Ignitions PDF Viewer, but nothing shows up.

How are you storing the pdf to the database? Are you doing it using Ignition? If so please post the script where you write the pdf file to the database.

Yep, I looked at the script last night and I was writing the path to the database and not the data, oops.

Old

[code]
path = system.file.openFile()

if path != None:
bytes = system.file.readFileAsBytes(path)
system.db.runPrepUpdate(“INSERT INTO documents (file) VALUES (?)”, [path])[/code]

New

[code]
path = system.file.openFile()

if path != None:
bytes = system.file.readFileAsBytes(path)
system.db.runPrepUpdate(“INSERT INTO documents (file) VALUES (?)”, [bytes])[/code]

Thanks for all the help!!