Ignition Image view in perspective

I Used file Upload component in perspective, after uploading in the same view, if I click on preview button it should show uploaded image .I tried passing

event.file.getBytes() 

data to Image source, but I am getting error. How I can solve this issue. Thanks in advance

Where are you putting the uploaded file? (In the onFileReceived event.)

If you are putting it in a file, you would use the WebDev module to serve it back out to clients.

If you are putting it in a database as a blob, you can use the WebDev module, but could also use my free Blob Server module.

yes, onFileReceived event. I am assigning it to some param .When I use that param it will return byteArray ..I am not getting ,how to bind that value to Image viewer source

You mean a Perspective property? That breaks byte arrays, as Perspective needs to JSONify everything.

Or you could Base64 encode it, and present with a data url. Not very efficient, but should work.

yes perspective property.(self.view.param.imageString)

I tried this
value = self.view.param.imageStream
b64Image = "data:imagef;base64,%s"%value
return b64Image

How can you use the modulo operator on a string?

Tip: please format code with the </> button.

Standard python string interpolation (old school). (I still use it everywhere--the "C" style placeholders are much closer to java implementations than the .format() method's curly braces.)

Yes, I've just looked it up. Is it appropriate in this case?

Yes, if the onFileReceived event stored the content in base64 (and it has to be in that event, not later).

1 Like

could you give one example .

If I assign it to some variable it will change it's format.So how I can encode it's value..

I would use java's Base64 static methods to encode in the onFileReceived event, and assign the encoded form (which is a string) to the Perspective image property. As a string, it won't be garbled by Perspective.

yes thanks,it's working now

import base64
self.view.params.imageStream = base64.b64encode(imageStream)

That would crash your designer for large images. In runtime, it should be ok. Store them in globals, the trade back it to properly managed persistency and ideally delete images ones used or timeout cycle.

That's a terrible idea, as is, as that becomes a memory leak if you don't clean up after yourself very thoroughly. Such a bad idea that I created the viewVarMap() set of functions to hold java/jython objects in context, with automatic cleanup. It is part of my (free) Integration Toolkit module.

I mentioned the trade back sir, for a good developer this should not be a terrible idea.

Yeah, no. That's the python standard library. Not recommended in jython for any technology that java already has. Put this in a project library script:

from java.util import Base64

commonB64encoder = Base64.getEncoder()

def bytesToB64(byteArray):
	return commonB64encoder.encodeToString(byteArray)

Then, your onFileReceived event can just do this:

	self.view.params.imageStream = someScriptModule.bytesToB64(imageStream)

This would be much more efficient.

[quote="Tokyo_mht, post:16, topic:86757"]
This works ,Thanks .But it won't work for large MB if i assign it to view param. If I use global variable it will work.