Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

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.