How to upload a file in gateway file system

hi ,
i want to upload a file in gateway file system to access the file in perspective.

i tried using file upload component . i just clicked browse in the component and uploaded my excel file .
where it will be stored? how can i retrive?

you have to store it somewhere yourself in the function onFileReceived
https://docs.inductiveautomation.com/display/DOC81/Perspective+-+File+Upload
https://docs.inductiveautomation.com/display/DOC81/system.file.writeFile
https://docs.inductiveautomation.com/display/DOC81/system.file.readFileAsBytes

i tried to read my file path as bytes using system.file.readFileAsBytes but i am getting the below error.

TypeError: readFileAsBytes(): 1st arg can’t be coerced to String

My code:
inputpath=system.db.runPrepQuery(“SELECT file_path FROM logbook_master_excel WHERE s_no=‘1’”)
inputFile=system.file.readFileAsBytes(inputpath)
print inputFile

even tried as:
inputByte=system.db.runPrepQuery(“SELECT file_byte FROM logbook_master_excel WHERE s_no=‘1’”)
inputFile=system.file.readFileAsBytes(inputByte)
print inputFile

this is not the correct syntax for a prepquery
https://docs.inductiveautomation.com/display/DOC81/system.db.runPrepQuery

and a prepquery does not return a string but a pydataset

Also if you are storing the filepath in the database, why not store the whole file there? xd

i have uploaded whole file as byte in database using file upload component in perspective. how can i retrive?
when i tried using readFileAsBytes() it is throwing the same error.
TypeError: readFileAsBytes(): 1st arg can’t be coerced to String

system.file.readFileAsBytes reads a file out of a folder on the gateway. where the argument has to be the filepath (string)

please provide the code inside the onFileReceived event

format it by first pressing </>

also what kinda files are you uploading and what do you want to do with it? You said an excel file? You want them to download the file again?

i am having a excel file uploaded in database . i want to read that file and update the data in required co-ordinates in excel and convert them into output file in local system.

When you run a query on your database table that contains the file in a blob column, the result dataset will have a byte array for that column’s dataset. There’s no file read needed at all. Just send the column value as the bytes in system.perspective.download.

Please use the </> code formatting button when posting code. It preserves indentation and you get free syntax highlighting. For inline portions of code you can `simply enclose in backticks`.

I to would like more information on this. The video on file upload shows us a few events, and SQL aspects that are on the manual/docs, but does not show where the file(s) are stored.

My goal is to use a property in another component that pulls the file/filepath and can use/open the file to do some operation in another aspect.

EX:

import csv


filepath = {fileUploadProperty}

open with(filepath, 'r') as file:
    readIN - csv.reader(file, delimiter=',')
    for row in readIN:
        col1 = row[0]
        system.perspective.print(col1)

The above is a simple printing of all row values for column one, hopefully using the file upload path.

They aren't stored for you. (They exist in a temporary file that isn't available after the event.) You must store them to a file yourself if you want that. Or placed in a database if that is more practical for you. Consider the uploaded file to be just in memory until you do something with it. (And that could be true, for small files. It is an implementation detail of Ignition's webserver.)

I don't use python's CSV module, so I don't know if it can parse directly from the file byte array.

2 Likes
2 Likes