How to save the uploaded file in default image folder (/system/images/)?

I am using file upload component to upload file and i want to save uploaded file on default image folder (/system/images/). I wrote the following code on onFileReceived event in file upload component:
upload_path = ‘/system/images/’
system.util.getLogger(‘FILEUPLOAD’).info(‘Uploading: ‘{0}’ to directory ‘{1}’’.format(event.file.name, upload_path))
event.file.copyTo(upload_path + event.file.name)

Thank you

This won’t work the way you’re expecting it to. The /system/images/ route handler isn’t attached to an actual folder in the gateway install directory - it’s handled entirely in code, and retrieves images from the internal database. If you want to manually upload images, you’ll need to get a reference to the ImageManager on the gateway and call insertImage manually, filling in all the appropriate fields:
com.inductiveautomation.ignition.gateway.images.ImageManager#insertImage. Actually doing so is unsupported, so you’re entirely on your own to figure out the details. Some searching on the forum should tell you how to get the GatewayContext class, from which you can getImageManager.

1 Like

Or you could use this other “unsupported” avenue:

1 Like

If I recall correctly, files placed in /webapps/main are only noticed on gateway restart, not dynamically…

Thank you so much it worked!
Is there any way that I can reduce the uploaded image file size?
I wrote something as follows which it seems not working:

upload_path = ‘webserver/webapps/main/’
system.util.getLogger(‘FILEUPLOAD’).info(‘Uploading: ‘{0}’ to directory ‘{1}’’.format(event.file.name, upload_path))
event.file.copyTo(upload_path + event.file.name)

import cv2

img=cv2.imread(upload_path + event.file.name)

scale_percent=0.50
width=int(img.shape[1]*scale_percent)
height=int(img.shape[0]*scale_percent)
dimension=(width,height)

resized=cv2.resize(img,dimension,interpolation=cv2.INTER_AREA)

print(resized.shape)
cv2.imshow('output',resized)
cv2.imwrite('resized_2.jpg',resized)![w|690x367](upload://tWcZkroKiMnNw0RzWRZeyPXTXEi.png)

Uh, that’s OpenCV, which needs native code support. They python bindings for OpenCV do not work in jython.

Is there any other way then that I can reduce the uploaded file size?

Try the corresponding functionality in Java’s own ImageIO library.

Why is this considered ‘unsupported’? Are there any drawbacks to this approach, i.e. using too many of the Gateway’s resources?

I don’t think files placed there are placed into any recovery backup. Also, most of the time when you want to use a file after it’s been uploaded (like a profile picture for example), you would want to serve that file as a resource through the WebDev module, which is not what this route is doing; this route id just directly placing a file into the file system.

It’s unsupported, because if you use it and something goes wrong, you’re on your own. We never explicitly set out to make the webserver/webapps/main path automatically hosted - it’s just a coincidence of the way we set up Jetty. Some future upgrade of Jetty may make that work differently, or we could explicitly gate the feature behind a config flag, or something.

In practice, because it’s such an open secret, we’d probably take pains to prevent anything from breaking it upon such a hypothetical Jetty upgrade, but the reason we claim it’s ‘unsupported’ is because you’re basically relying on undefined Jetty behavior, not anything coded into Ignition deliberately.

1 Like