Download selected attachments into zip
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2020 11:11 PM
Hi all,
Requirement is such: Download only selected attachments in zip format
Created ui action
Created ui page which is triggered by ui action
Downloaded files one by one which were selected.
But all these have to be in a zip file. As shown in https://www.servicenowguru.com/scripting/download-attachments-zip-file/ , it has a ui action which queries the attachments table based on sys_id of the current record.
This did not help me do it. The ui action triggers the processor and files are downloaded. I tried to add the same script in ui page client script that i created.
function downloadFiles(){
var selected = [];
$j("input:checkbox[name=type]:checked").each(function() {
selected.push($j(this).attr('id'));
});
alert(selected);
var gr=[];
var num = selected.length;
var zipName = 'attachments.zip';
var StringUtil = GlideStringUtil;
while(num>0){
gr[num-1]= selected[num-1];
--num;
}
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();
}
This did not work, as the action.setRedirectURL('exportAttachmentsToZip.do?sysparm_sys_id=' + current.sys_id + '&sysparm_table=' + current.getTableName()); link isn't specified in the ui action and i haven't used the processor.
Any help on this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2020 12:14 AM
Hi Deepika,
I think the processor needs to be present. Did you create the components as per the link?
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2020 12:30 AM
I created the processor, but i think the data from ui page should be transferred to processor that which files are selected.
Am i right?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2020 02:39 AM
Can i pass data from client script in the ui page to the processor?
The processor contains script which downloads all the files in zip, so when the selected attachments(in the form of array, if am right) are sent to processor, it zips only those attachments.