# Using SQL images (byte arrays) in reports

**URL:** <https://forum.inductiveautomation.com/t/using-sql-images-byte-arrays-in-reports/93321>\
**Category:** Ignition\
**Tags:** database, ignition81, perspective, reporting\
**Created:** [July 17, 2024, 9:08am UTC](https://forum.inductiveautomation.com/t/using-sql-images-byte-arrays-in-reports/93321 "2024-07-17T09:08:35Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luca\_Hofmann](https://avatars.discourse-cdn.com/v4/letter/l/85e7bf/32.png) [@Luca\_Hofmann](https://forum.inductiveautomation.com/u/Luca_Hofmann)\
**Post date:** [July 17, 2024, 9:08am UTC](https://forum.inductiveautomation.com/t/using-sql-images-byte-arrays-in-reports/93321/1 "2024-07-17T09:08:35Z")

</div>

Hi,  
i need to display images that are stroed as byte arrays in MSSQL in a Report. From what ive seen so far, the image component in reports needs a path as key. Is there a way to display byte array images in reports? I know how to do it in perspective.

There was a similiar post 2 years ago without solution:

> [@How to view the in-Ignition reports using byte array instead of image path?](https://forum.inductiveautomation.com/t/how-to-view-the-in-ignition-reports-using-byte-array-instead-of-image-path/61978):
>
> [image]

Im thankful for any input  
Luca

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 17, 2024, 3:39pm UTC](https://forum.inductiveautomation.com/t/using-sql-images-byte-arrays-in-reports/93321/2 "2024-07-17T15:39:26Z")

</div>

Set your byte array as a parameter or data key value (via script or query) and then bind the image component to that key, and it should "just work".

---

<div class="post-metadata">

**Author:** ![Luca\_Hofmann](https://avatars.discourse-cdn.com/v4/letter/l/85e7bf/32.png) [@Luca\_Hofmann](https://forum.inductiveautomation.com/u/Luca_Hofmann)\
**Post date:** [July 18, 2024, 8:07am UTC](https://forum.inductiveautomation.com/t/using-sql-images-byte-arrays-in-reports/93321/3 "2024-07-18T08:07:11Z")

</div>

Hi, i tried that already and it doesnt work. Not sure if i am missing something.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/3/63031f8422ff291e4b129712842d2b76df5f10dd.png)

The bytecode is saved in "emptyCellForPic". Here is what it looks like in preview:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/1/61fb74cbaa7d45ac8d3527765c277c2acf1d1c6e.png)

Maybe i have the bytecode in wrong format?  
Thank you.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 18, 2024, 1:19pm UTC](https://forum.inductiveautomation.com/t/using-sql-images-byte-arrays-in-reports/93321/4 "2024-07-18T13:19:52Z")

</div>

What's the actual source of that data? You want a raw Java byte array that contains the binary data for an image (in JPG, BMP, PNG, or a few other formats), not any kind of encoded data.

---

<div class="post-metadata">

**Author:** ![Luca\_Hofmann](https://avatars.discourse-cdn.com/v4/letter/l/85e7bf/32.png) [@Luca\_Hofmann](https://forum.inductiveautomation.com/u/Luca_Hofmann)\
**Post date:** [July 18, 2024, 1:28pm UTC](https://forum.inductiveautomation.com/t/using-sql-images-byte-arrays-in-reports/93321/5 "2024-07-18T13:28:21Z")

</div>

The image (JPG) is uploaded via the perspective FileUpload Component and a "onFileReceived" action wich utilizes a skript. In that script i use event.file.getBytes() to get the imagebytes and save that to a database (named Query). In the report i query that db for the bytevalue.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 18, 2024, 1:29pm UTC](https://forum.inductiveautomation.com/t/using-sql-images-byte-arrays-in-reports/93321/6 "2024-07-18T13:29:31Z")

</div>

And the DB column is a BLOB type, and nothing's getting truncated? What does the data look like in the XML preview in the report designer?

---

<div class="post-metadata">

**Author:** ![Luca\_Hofmann](https://avatars.discourse-cdn.com/v4/letter/l/85e7bf/32.png) [@Luca\_Hofmann](https://forum.inductiveautomation.com/u/Luca_Hofmann)\
**Post date:** [July 18, 2024, 1:41pm UTC](https://forum.inductiveautomation.com/t/using-sql-images-byte-arrays-in-reports/93321/7 "2024-07-18T13:41:37Z")

</div>

Image Type (MSSQL)  
XML Preview:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/e/8e65a8fe06dc55c9d869709a5fe6cbdda249bdd9.png)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/7/674792e0924d730d96a57223c85a4e36e6583013.png)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 18, 2024, 1:49pm UTC](https://forum.inductiveautomation.com/t/using-sql-images-byte-arrays-in-reports/93321/8 "2024-07-18T13:49:05Z")

</div>

It looks like there's some double encoding happening - you shouldn't be getting that leading XML like string if you're uploading the raw image data. Can you post your file upload script?

---

<div class="post-metadata">

**Author:** ![Luca\_Hofmann](https://avatars.discourse-cdn.com/v4/letter/l/85e7bf/32.png) [@Luca\_Hofmann](https://forum.inductiveautomation.com/u/Luca_Hofmann)\
**Post date:** [July 18, 2024, 1:52pm UTC](https://forum.inductiveautomation.com/t/using-sql-images-byte-arrays-in-reports/93321/9 "2024-07-18T13:52:40Z")

</div>

```python

def runAction(self, event):
	# Upload des Files (Bild im jpg Format über File Upload Component, zuordnung jpg Bytes zu variable image
	image = event.file.getBytes()
	
	args =	{
		"assetID": self.view.params.assetID,
		"categoryID": 1,
		"imageName": self.parent.parent.getChild("txtf_nameInput").props.text,
		"imageByte": image
	}
	
	resp = system.db.runNamedQuery("INSERT/addPicOld",args)
	
	self.parent.parent.getChild("img_image").custom.imageID = resp.getValueAt(0, "imageID")

```

---

<div class="post-metadata">

**Author:** ![Luca\_Hofmann](https://avatars.discourse-cdn.com/v4/letter/l/85e7bf/32.png) [@Luca\_Hofmann](https://forum.inductiveautomation.com/u/Luca_Hofmann)\
**Post date:** [July 18, 2024, 1:56pm UTC](https://forum.inductiveautomation.com/t/using-sql-images-byte-arrays-in-reports/93321/10 "2024-07-18T13:56:04Z")

</div>

The Select Query might be the issue (gets me the data from the db to the report)

```python
select imageBytes
from images 
where imageID = :imageID 
FOR XML AUTO, BINARY BASE64;

```

Looks like this if i remove "FOR XML AUTO, BINARY BASE64;"

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/0/a02b95d07c27d2aa8d5a91797935c6b0cd442559.png)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 18, 2024, 2:01pm UTC](https://forum.inductiveautomation.com/t/using-sql-images-byte-arrays-in-reports/93321/11 "2024-07-18T14:01:40Z")

</div>

> [@Luca\_Hofmann](#):
>
> Looks like this

That looks more like what I would expect. Does that show up correctly in the image component?

---

<div class="post-metadata">

**Author:** ![Luca\_Hofmann](https://avatars.discourse-cdn.com/v4/letter/l/85e7bf/32.png) [@Luca\_Hofmann](https://forum.inductiveautomation.com/u/Luca_Hofmann)\
**Post date:** [July 18, 2024, 2:02pm UTC](https://forum.inductiveautomation.com/t/using-sql-images-byte-arrays-in-reports/93321/12 "2024-07-18T14:02:46Z")

</div>

Nope 😕

---

<div class="post-metadata">

**Author:** ![Luca\_Hofmann](https://avatars.discourse-cdn.com/v4/letter/l/85e7bf/32.png) [@Luca\_Hofmann](https://forum.inductiveautomation.com/u/Luca_Hofmann)\
**Post date:** [July 18, 2024, 2:15pm UTC](https://forum.inductiveautomation.com/t/using-sql-images-byte-arrays-in-reports/93321/13 "2024-07-18T14:15:38Z")

</div>

I found my issue:

If i dont take the value at (0,0) and instead just use the result my Select Query gives me, its shown properly.

```python
#data['test'] = imageBytes.getValueAt(0,0)
data['testraw'] = imageBytes

```

I assigned the select results to "imageBytes" and then took the value at first row, first column. Somehow that was the problem, if i just use the query result as a whole it works ? 😃

If you know why thats works im interested in that.

anyway, thank you very much for your Help!!

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 18, 2024, 2:17pm UTC](https://forum.inductiveautomation.com/t/using-sql-images-byte-arrays-in-reports/93321/14 "2024-07-18T14:17:54Z")

</div>

Huh, maybe we're automatically doing something in the image component with the (presumably) dataset we're now getting, but I would've thought the same as you, that the right thing to do would be to either unpack in the script or make your query to fetch the image data a scalar query. Either way, working is working 😄
