Download attachments

Palak Midha
Tera Contributor

Hi all,

I am trying to download attachments using UI Action.

The UI action looks like below attached image.

I have tried g_navigation.openPopup, window.open, action.setRedirectURL  and also GlideDialogWindow.

Also when using window.open, the property "glide.script.block.client.globals" was false. Still, the attachments aren't downloaded when the UI action is clicked.

The attachment for one record was downloaded when action.setRedirectURL was used.

 

Kindly assist how can I get this working.

7 REPLIES 7

Community Alums
Not applicable

Hi @Palak Midha ,

You can use the same process as mentioned in this link. With slight modifications, you can get to download all attachments.

https://www.servicenowguru.com/scripting/download-attachments-zip-file/#comment-34336

Please note that the script might take time and if you want to limit to N number of records, you can do so in the below script. 

Few points to Note

1. The count variable is added to the file name to avoid duplicate file names. If there are duplicate file names, the zip process will break.

2. The script is modified to simply query sys_attachment and download them in the processor. It is not capturing any parameters from UI action as such. 

3. Create the UI action as a form button on any incident or any other table.

  var zipName = 'attachments.zip';
  var StringUtil = GlideStringUtil;
  var gr = new GlideRecord('sys_attachment');
  gr.query();
  if (gr.next()){
    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); 
        //add counter so that the filename is NOT duplicate
        var file = gr.file_name+"-"+count; 
	gs.info("Entered into this loop"+file);
	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();
}

The script takes time to download all the attachments. To test the script, i suggest you break the loop above at 5 or 10 records.

Mark my answer correct & Helpful, if Applicable.

Thanks,

Sandeep

joshuastanley
Tera Expert

The above methods does not work any more. Thats an old code shared 10year back LOL

Igor __tek
Tera Contributor

Hi @Palak Midha ,
my solution is like this. Just don't forget to set the "Isolate Script" chackbox to false.
Related list UI - Action script:

function downloadAllSelectedAttachments() {
    var listOfIds = g_list.getChecked();
    if(listOfIds.length == 0) {
        alert("You didn't select any attachment");
    } else {
        if (listOfIds.length == 32) {
            window.location.href ="/sys_attachment.do?sysparm_referring_url=tear_off&view=true&sys_id=" + listOfIds;
        } else {
            var arrOfIds = listOfIds.split(",");
            arrOfIds.forEach(function(att_sys_id) {
                var url ="/sys_attachment.do?sysparm_referring_url=tear_off&view=true&sys_id=" + att_sys_id;
                window.open(url, '_blank');

            });
        }
    }
}