I am currently trying to write a script on my file upload component. However, when I pass in the uploaded data it does print out using system.perspective.print() in the format I want (scalar). However when I try to save or store the data further in the script, it still remains in its original byte dataset format. Is there any way I can convert it to the scalar version for storage based purposes? I do not want a 2MB array on my ignition.
I want this which is printed out:
But I keep getting this:
event.file.getBytes()
, as documented, returns a raw Java byte array. There is no more fundamental type for it to be returned as - it's literally the binary data that was the uploaded file.
What that format lacks is a specific string presentation, because turning binary bytes into a string is an encoding operation, and there are infinitely many possible ways you could encode those bytes.
You probably, if you are storing into a database or as a file, do not want to do any encoding. You should only encode if you actually have to output as a string somewhere, and you must choose a format that can reliably encode arbitrary bytes, such as base64; if you try to ASCII or UTF-8 encode a random binary file you'll get at best garbage and at worst lose significant portions of your data.
Show the rest of the script you're actually using, and describe where you're trying to store these uploaded files.
Sorry for the late reply, I am using this data to create a form for people to fill out and at the end add images or PDFs to it.
My main concern with the byte array compared to the other is that one is 2MB of data while the other is 577 kB and I do not want to bog down the gateway. Is there a better alternative to what I am trying to accomplish?
I suspect you are assigning the byte array to a Perspective property. That breaks it. Perspective properties cannot hold arbitrary objects, and your byte array is being wrapped into a Perspective array.
Put the original byte array directly into your database. Do not access it in Perspective at all. Rely on the WebDev or Blob server to deliver bytes to your Image or IFrame components.
Oh ok so I should create a temporary holding db to store the image data and when the user submits the form transfer the info to the main db and delete it from the temporary db?
I wouldn't transfer. Make a table just for image data. Four columns: ID, timestamp, content, and content type (to support different image formats). Carry the ID in your form progress, not the bytes. At form submission time, include the ID in the main table for the process as a foreign key to the images table.
For images that get uploaded but never attached to a completed form, run a clean up query that deletes images that aren't referenced from the main table and are some number of days old.
(Maybe a fifth column for the user who uploaded. So you can report how many abandoned images belong to particular users.)
2 Likes
It worked great! Thank You!