GlideSysAttachment getBytes is not working if size of attachment is more than XX MB

Knight-for-dest
Tera Contributor

I attempted to use the following code to convert an attachment into Base64 format. However, when the attachment exceeds a certain size, the length of the return value from getBytes becomes 0.

If anyone knows the cause of this issue or has a better solution, I would greatly appreciate your guidance. Thank you!



SendFile: function(sys_id) {

var attachment = new GlideSysAttachment();

var gr = new GlideRecord('sys_attachment');

gr.addQuery('sys_id', sys_id); gr.query();

if(gr.next()){ var stringUtil = new GlideStringUtil();

var data = attachment.getBytes(gr);

} else {

this.errorHandler.UnknownError('CSBIFFileAttachment', 'SendFile', 'Not Found attachment:'+sys_id); }

return stringUtil.base64Encode(data);

},

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Knight-for-dest 

I believe the limit is 5MB

this link has solution shared by me but it works for global scope only

GlideStringUtil.base64Encode is not converting large files to base64encode string. 

try this

SendFile: function(sys_id) {

    var StringUtil = new GlideStringUtil();
    var gsis = GlideSysAttachmentInputStream(sys_id);
    var ba = new Packages.java.io.ByteArrayOutputStream();
    gsis.writeTo(ba, 0, 0);
    ba.close();
    var base64EncodedData = StringUtil.base64Encode(ba.toByteArray());
    return base64EncodedData;
},

Controlled via system property, but not recommended to update due to memory issues -> com.glide.attachment.max_get_size -> if this property is not present then you can create it and give size in bytes (for example 7000000 for 7MB)

How to read attachments with more than 5 MB size, in Scoped Application ? 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Knight-for-dest 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader