Upload an excel file/attachment from UI Page to script include to store as attachment in table

too_hoppy
Tera Contributor

I am trying to upload an excel file as an attachment from a UI Page and then use an ajax call to a script include which will store that data as an attachment. My code is currently creating an attachment but the data I sent over seems corrupted (most likely not sending the data correctly or incorrectly writing it on the back end). Here is code snippets for both client script and server include script:

 

client snippet:

var fileReader = new FileReader();
fileReader.onloadend = function(event) {
var ga = new GlideAjax('poCatItemUtil');
ga.addParam('sysparm_name', 'uploadAttach');
ga.addParam('sysparm_filename', file.name);
ga.addParam('sysparm_contenttype', file.type);
ga.addParam('sysparm_data', fileReader.result);
ga.getXML(getResponse);
};

fileReader.readAsBinaryString(file);

 

server snippet:

var filename = this.getParameter('sysparm_filename');
var contentType = this.getParameter('sysparm_contenttype');
var data = this.getParameter('sysparm_data');

var cat_item = new GlideRecord('x_tpw_invoice_po_0_po_cat_item');
cat_item.name = ('tmpPODivItem');
cat_item.active = ('true');
cat_item.insert();

var attachment = new GlideSysAttachment();
var att1 = attachment.write(cat_item, filename, contentType, data);

var cat_item_sys_id = cat_item.sys_id;

 

It creates the attachment but when I download the attachment and try to open it I get a corrupted/unreadable excel file. Any suggestions would be appreciated. Thank you.

0 REPLIES 0