Unable to upload files

I am trying to upload files in Ignition Perspective but when calling event.file.getBytes() I am getting an array which is causing the insertion into the database to fail as it is expecting varbinary(MAX). I am working with the example provided in the docs.

def runAction(self, event):
	from traceback import format_exc
	
	log = system.util.getLogger("Photo Upload")
	
	try:
		filename = event.file.name
		filedata = event.file.getBytes()
		
        # Output: <type 'array.array'>
		log.info(str(type(filedata)))
	
	except Exception:
		log.error(format_exc())
	
	# Use a query to insert the file
#	query = "INSERT INTO photos (filename, photodata) VALUES(?, CONVERT(varbinary(MAX), ?))"
#	args = [filename, filedata]
#	db = self.session.custom.dbConnection
#	system.db.runPrepUpdate(query, args, db)

image

Side note: I think storing files in the database is disgusting so if anyone has tips on where I should store the files using system.file.writeFile()

Yes, bytes() is an array. Of bytes. :man_shrugging:

Show your complete script with SQL.

Most JDBC drivers are perfectly happy to send java byte arrays to databases using "Prep" queries (? substitutions) or named queries (value parameters).

Try putting millions of small files in a folder in a filesystem, and compare to putting small blobs in a production-grade database. You'll change your mind, I suspect.

If you want to use files, and sometimes you should, you probably want to use the WebDev module to serve them to clients on demand.

1 Like