GlideSysAttachment().getBytes() is not working if size of attachment is more than 5 MB?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2020 09:15 AM
I have the below code written in the processor, can you please help in providing the solution so that attachments greater than 5MB also gets downloaded. Can you please provide the exact line of code change to be done. Thank you in advance.
(function process(g_request, g_response, g_processor) {
var sysid = g_request.getParameter('sysparm_sys_id');
var table = g_request.getParameter('sysparm_table');
var theRecord = new GlideRecord(table);
theRecord.addQuery('sys_id', sysid);
theRecord.query();
theRecord.next();
var zipName = 'attachments.zip';
var StringUtil = GlideStringUtil;
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', theRecord.sys_id);
gr.addQuery('table_name', theRecord.getTableName());
gr.query();
if (gr.hasNext()){
g_response.setHeader('Pragma', 'public');
g_response.addHeader('Cache-Control', 'max-age=0');
g_response.setContentType('application/octet-stream');
g_response.addHeader('Content-Disposition', 'attachment;filename=' + zipName);
var out = new Packages.java.util.zip.ZipOutputStream(g_response.getOutputStream());
var count=0;
while (gr.next()){
var sa = new GlideSysAttachment();
var binData = sa.getBytes(gr);
var file = gr.file_name;
addBytesToZip(out, zipName, file, binData);
count ++;
}
// Complete the ZIP file
out.close();
}
function addBytesToZip (out, dir, file, stream){
// Add ZIP entry to output stream.
out.putNextEntry(new Packages.java.util.zip.ZipEntry(file));
out.write(stream, 0, stream.length);
out.closeEntry();
}
})(g_request, g_response, g_processor);
I have seen posts mentioning about below :
https://community.servicenow.com/community?id=community_question&sys_id=ae11d7e9dbdcdbc01dcaf3231f961940
https://community.servicenow.com/community?id=community_question&sys_id=648a4be9db5cdbc01dcaf3231f961904
But , can I get the exact code change to be done for this. Please help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-25-2020 12:59 PM
/* Replace this bit
var sa = new GlideSysAttachment();
var binData = sa.getBytes(gr);
*/
//With this
var gsa = GlideSysAttachmentInputStream(gr.sys_id.toString());
var baos = new Packages.java.io.ByteArrayOutputStream();
gsa.writeTo(baos);
baos.close();
var binData = baos.toByteArray();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2021 06:43 AM
Hi
I hope you are doing well. I'm having this issue as well.
I tried the codes you provided, but I got a Stream Closed exception:
Evaluator: java.io.IOException: Stream closed
Caused by error in script at line 14
11: if(Gr.next()){
12: var gsa = new GlideSysAttachmentInputStream(sys_id);
13: var baos = new Packages.java.io.ByteArrayOutputStream();
==> 14: gsa.writeTo(baos);
15: baos.close();
16: var dataAsString = baos.toByteArray();
17: dataAsString = String(dataAsString);
Do you happen to know the cause of this issue? Thanks a lot in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2021 07:29 AM
After some investigation, I found I switched to use sys_attachment table to get the attachment file directly. But I'm still not getting the correct contents in the files.
Instead, I got some junk data like "[B@da2ef8"....
I would really appreciate your help!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2021 12:59 PM
A ByteArray isn't really human readable and looks like a bunch of junk data. So converting it to a string and writing it to the log isn't going to be that useful, other than proving you actually have something in the dataAsString variable.
I used the above method to read in attachments and add them to a *.zip file. If you are using this to POST attachments via a web service or REST ServiceNow now has a function specifically for doing this "setRequestBodyFromAttachment", which is much simpler to use. For example:
var sm = new sn_ws.RESTMessageV2('[REST Message Name Here]','POST Attachments');
sm.setRequestBodyFromAttachment(sys_attachment.sys_id.toString());
