Image attachment to byte array

wclim
Kilo Sage

I am able to send over text files properly, but I cant send non-text files like images.
Is there any way I can send the contents of an image file over?

 

Code I am using:

var gsa = new GlideSysAttachment();
var data = gsa.getBytes(attachmentRecord);

//convert to byte array
var dataString = String(Packages.java.lang.String(data));
var byteArray = [];
for (i = 0; i < dataString.length; i++) {
byteArray.push(dataString.charCodeAt(i));
}


//Send byteArray as Payload to external system

E.g. Working example for text files

1) I have a text file containing "HELLO!"
2) The file byte array sent over is [72 69 76 76 79 33 ]
3) File get sent over successfully

E.g. Error for image files

1) I have a image file
2) The file byte array send over is [65533,65533,65533,65533,0,16,74,70,73,70,0,1,1,1,0,96,0,96,0,0,65533,65533,0,67.......many more bytes]
3) Error encounter: There was an error deserializing the object of type System.Byte[]. The value '65533' cannot be parsed as the type 'Byte'.

I suspect that it is because the image contains unrecognized characters like � when trying to convert into the array thereby giving this error. So I would like to know there is anyway to convert an image to a byte array properly on servicenow side?

1 ACCEPTED SOLUTION

wclim
Kilo Sage

I did not manage to get it to work, so I have to get the external system to provide an API that accepts base64 format instead of file byte array.

View solution in original post

5 REPLIES 5

Ashutosh Munot1
Kilo Patron
Kilo Patron

HI,

You have to first convert the image into base64 and then use it.


Thanks,
Ashutosh

wclim
Kilo Sage

Hi Ashutash,

 

I did try encoding it in base64 and sending it over, but what I get is that the image did not get sent correctly and is corrupted.

The external system REST API only accepts the FileByte array in decimal format, and not base64 format. 

Is that why I am unable to send the image contents over?

Hi,

 

We have to check if rest can support image conversion to fie byte or not.


Thanks,
Ashutosh

R_ Alan Rocco
Kilo Explorer

I'm experiencing the exact same issue, did you find a solution eventually?