Reading/Writing Files to the Database

Yes, the line 16 in my code doesnt refernce event. All the try… except does is checks for the files window to be open, and if its open, it brings the window to the front, and updates the list. If the windows is not open, it just pops up a confirmation window to tell you the file has finished uploading. I want to try make a progress bar for the upload, but a) have no idea how, and b) dont have time right now to figure it out. Its something i will look at in the new year maybe, unless Carl knows of a way to do this.

Uploading the most recent window now
File Management.fwin (10.3 KB)

You currently don't have any sort of ability to do something like this. We'd have to give you some sort of feedback as the client uploads the bytes to the Gateway, but you'd never get it all that accurate, as you're also at the mercy of the Gateway inserting the data over JDBC, which getting feedback for would be quite hard. Some sort of "please wait" animated screen would of couse be easy, but true progress bar, not so much.

I'll put a feature request for such feedback on the "Someday" list

[quote=“Carl.Gould”]
Sorry, there were too many scripts posted here for me to re-create exactly what script you’re using. Please post the script.[/quote]

Ok, here is the code that works:

#get tag of window 
tag = event.source.parent.Tag 
#prompt user to select file 
path = fpmi.file.openFile() 
#if user selects a file, and does not click cancel, do this 
if path != None: 
	import fpmi
	ext = path[path.rfind(".")+1:]
	name = path[path.rfind("\")+1:len(path)-len(ext)-1]
	#read in the file as bytes 
	bytes = fpmi.file.readFileAsBytes(path) 
	#write the data to the database, and return the id of the new row 
	fpmi.db.runPrepStmt("INSERT INTO files (tag,name,ext,data) VALUES (?,?,?,?)", (tag,name,ext,bytes)) 
	#refresh the data in the list, as it is set to polling off 
	fpmi.db.refresh(event.source.parent.getComponent('List'),"data") 

This is the code that doesn’t work:

#get tag of window 
tag = event.source.parent.Tag 
#prompt user to select file 
path = fpmi.file.openFile() 
#if user selects a file, and does not click cancel, do this 
if path != None: 
	def insertFile(path = path, tag = tag):
		import fpmi
		ext = path[path.rfind(".")+1:]
		name = path[path.rfind("\")+1:len(path)-len(ext)-1]
		#read in the file as bytes 
		bytes = fpmi.file.readFileAsBytes(path) 
		#write the data to the database, and return the id of the new row 
		fpmi.db.runPrepStmt("INSERT INTO Files (tag,name,ext,data) VALUES (?,?,?,?)", (tag,name,ext,bytes)) 
		#refresh the data in the list, as it is set to polling off 
		fpmi.db.refresh(event.source.parent.getComponent('List'),"data") 
	fpmi.system.invokeAsynchronous(insertFile)

I haven’t tried the new code that Kyle just posted yet. I just want to understand what is going on with the above code first. I’m not a Python expert (yet).

Whats going on is that the block that doesn’t work is trying to run the work through a call to “fpmi.system.invokeAsynchronous”. This means that it will be done in the background, allowing the button “unpress” and the GUI to keep responding.

Doing this involves wrapping the code in a new function (def insertFile). When you create a new function, the code in that function creates a new scope. This means that the use of the variable “event” in the second to last line is illegal, because there is no variable named “event” in insertFile’s namespace. You can pass event into the namespace in the def, like this:

#get tag of window tag = event.source.parent.Tag #prompt user to select file path = fpmi.file.openFile() #if user selects a file, and does not click cancel, do this if path != None: def insertFile(path = path, tag = tag, event=event): import fpmi ext = path[path.rfind(".")+1:] name = path[path.rfind("\\")+1:len(path)-len(ext)-1] #read in the file as bytes bytes = fpmi.file.readFileAsBytes(path) #write the data to the database, and return the id of the new row fpmi.db.runPrepStmt("INSERT INTO Files (tag,name,ext,data) VALUES (?,?,?,?)", (tag,name,ext,bytes)) #refresh the data in the list, as it is set to polling off fpmi.db.refresh(event.source.parent.getComponent('List'),"data") fpmi.system.invokeAsynchronous(insertFile)

Hope this helps,

Beat me to it Carl. THe updated window i recently posted included the event = event in the call.

Step7, if you make any changes to the screen, please post it back to the forum for some insight or features i may have missed