How can I download multiple attachments as single files instead of ZIP?

Tobias Persson
Tera Contributor

Hi community!

I would like to set up a UI Action that downloads all attachments. I know there is a functionality that does this downloading all files as a ZIP file. But that's not what I want. I need every file downloaded as is without having it in a ZIP file. 

Is that possible and how would I do that?

Thanks!

1 ACCEPTED SOLUTION

Hi,

can be done

1) create a form UI action which is client side

2) Client checkbox - true

3) Onclick - downloadAll()

4) Script:

Ensure your pop-up blocker is allowing the new tabs then only the files would be downloaded

function downloadAll() {

	var gr = new GlideRecord("sys_attachment");
	gr.addQuery("table_sys_id", g_form.getUniqueValue());
	gr.query();
	while(gr.next()) {
		window.open('/sys_attachment.do?sys_id='+gr.sys_id,"_blank");
	}
}

find_real_file.png

Regards
Ankur

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

View solution in original post

12 REPLIES 12

Hi,

thank you! I now changed the correct answer to Ankurs answer because in the end he showed the steps even if the script wasn't exactly correct for what I needed.

As I am new to the community I am not sure if that is the correct way because - as I said in my reply to Ankurs question why I didn't mark his answer as correct and chose mine instead - the 100% correct script is in my own reply. But Ankur should of course get his points for the help he provided so I'll leave it this way now and maybe I'll add my answer again to the overview page as it is hidden so deep down the thread here and can be overseen easily by others.

Tobias Persson
Tera Contributor

My UI Action scripts that solved my scripting problems on this question are nested pretty deep in the thread so I thought I'd repost/copy it at the "top" of this question so you won't have to search. Ankurs script solution  in step 4 didn't work for me as I am triggering this Client Side. Here's how I solved it in the end:

 

// Script Include (server side)
var MyAjax = Class.create();
MyAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
  
  getAttachments: function() {
    var reportSysID = this.getParameter('sysparm_report_sys_id');
    var fileURLs = [];
    var gr = new GlideRecord("sys_attachment");
    gr.addQuery("table_sys_id", reportSysID);
    gr.query();
    while(gr.next()) {
      fileURLs.push('/sys_attachment.do?sys_id='+gr.sys_id);
    }

    return JSON.stringify(fileURLs);
  },

  type: 'MyAjax'
});

 

// UI Action function (client side)
function downloadAll() {
  try {
    // run server side logic
    var ajax = new GlideAjax('MyAjax');
    ajax.addParam('sysparm_name', 'getAttachments');
    ajax.addParam('sysparm_report_sys_id', g_form.getUniqueValue());
    ajax.getXML(runDownload);
  } catch (e) {
    alert(e.message);
  }
}

// callback function for ajax call
function runDownload(response) {
  try {
    var files = response.responseXML.documentElement.getAttribute("answer");
    files = JSON.parse(files);
    for (var i = 0; i < files.length; i++) {
      top.open(files[i], '_blank');
    }
  } catch (e) {
    alert(e.message);
  }
}

 

Hello Tobias,

 

Can you provide the step by step process to add download all attachment in service operation workspace in servicenow