Decode base64 field and add to record as attachment

rcard11
Tera Guru

I have a field on a case form that is populated with a base64 value.  I would like to take that string and convert it back into an image and attach that image to the case.  Has anyone had any success in doing this?  Thank you in advance for your help!

10 REPLIES 10

@Ankur Bawiskar Has anyone found the solution to this ? Thanks in advance.

Hi Ankur,

I have same issue of @rcard11 

I am able to attach the attachment to target case record but attachment is not supporting.

Vinicius Luz1
Tera Expert

@rcard11  hey could you make it work somehow ?

Vanderlei
Mega Sage

Hi @rcard11 @Vinicius Luz1 @Shreya  , try this code 

 

var attachment = new GlideSysAttachment();

/*If is a Business Rule, you can use the current */
var current= new GlideRecord('case_table');
var caseSYSID= 'case_sys_id';
current.get(case_sys_id);

var fileName = 'example.txt';
var contentType = 'text/csv';
var base64Encodedcontent = current.base64_field

var agr = attachment.writeBase64(rec, fileName, contentType, base64Encodedcontent);

gs.info('The attachment sys_id is: ' + agr);

 

 

If my answer helped you, please mark my answer as helpful.

 

Vanderlei Catione Junior | LinkedIn

Senior ServicePortal Developer / TechLead at The Cloud People

gabez
Tera Contributor

I am working with a similar issue, but my contentType is "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" for an XLSX file, but its not working. 

 

Below is a portion of the code where 'contents' contains a base64 string that should be ok and work properly:

 

		var fileName = fname + ".xlsx";

		var gr = new GlideRecord('sys_properties');
		gr.initialize();
		gr.setValue('property_name', 'excel');
		gr.insert();
		var record_sys_id = gr.sys_id;

		var rec = new GlideRecord('sys_properties');
		rec.get(record_sys_id);

		var contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';

		var base64Encodedcontent = contents;


		var attachment = new GlideSysAttachment();

		var agr = attachment.write( rec, fileName, contentType, base64Encodedcontent);


		action.setRedirectURL("/sys_attachment.do?sys_id=" + agr);
		gs.info('The attachment sys_id is: ' + agr);