Issue with base64 image after conversion

Hari1
Mega Sage

Hi,

I have an issue after the base64 string is converted to an image and attached to the incident record. The image is blank. Please find the below screenshot.

Hari1_0-1725882943644.png

 

Below is the code that i am using:

 

Client Script - OnChange:

var ajx = new GlideAjax('AttachRec');
ajx.addParam('sysparm_name', 'AttachInc');
ajx.addParam('url', dataURL); // Base64 URL
ajx.addParam('inc', g_form.getUniqueValue()); //incident record sys_id
ajx.getXML(attachImg);

function attachImg(response) {
           var answer = response.responseXML.documentElement.getAttribute("answer");
           g_form.addInfoMessage("Attachment added successfully and answer: " + answer);
}

Script Include:

var AttachRec = Class.create();
AttachRec.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    AttachInc: function() {

        var incUrl = this.getParameter('url');
        var incSysId = this.getParameter('inc').toString();
        var base64Bytes = GlideStringUtil.base64DecodeAsBytes(incUrl);
       
        var rec = new GlideRecord('incident');
        rec.get(incSysId);

        var attachment = new GlideSysAttachment();
	var fileName = "newImg.png";
        var contentType = '';
        var agr = attachment.write(rec, fileName, contentType, base64Bytes);
       
       return attachment.sys_id;
    },
    type: 'AttachRec'
});

 Thanks

5 REPLIES 5

anchal1234
Tera Contributor

Hi @Hari1 ,

 

Can you please elaborate more on the data URL, what exactly you are getting there .

 

 

Regards,

Anchal Jha

Daniel Madsen
Kilo Sage

Is the parameter "dataUrl" a url for an image, or is it the actual base64 encoded data of the image? Based on the names of the variables, it looks like you are trying to base64 decode a url, which is then added as an attachment. Try passing in the base64 encoded data instead.

@Daniel Madsen @anchal1234 

I have added the dataUrl string text check the base64 url text that i am passing. Please find the attached text file.

Thanks,

Rajesh Chopade1
Mega Sage

Hi @Hari1

In the GlideSysAttachment.write() method, you're passing an empty contentType variable. This needs to be the correct MIME type (e.g., image/png, image/jpeg) for the image to display properly.

 

Update your script as bellow and try once:

Client script

 

var dataURL = canvas.toDataURL("image/png"); // Or other method generating base64
var ajx = new GlideAjax('AttachRec');
ajx.addParam('sysparm_name', 'AttachInc');
ajx.addParam('url', dataURL); // Base64 URL including MIME type
ajx.addParam('inc', g_form.getUniqueValue()); //incident record sys_id
ajx.getXML(attachImg);

 

 

Script include

 

var AttachRec = Class.create();
AttachRec.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    AttachInc: function() {
        var incUrl = this.getParameter('url');
        var incSysId = this.getParameter('inc').toString();

        // Extract the Base64 data and MIME type
        var base64Index = incUrl.indexOf('base64,') + 7; // Find where base64 data starts
        var base64String = incUrl.substring(base64Index); // Base64 string without prefix
        
        // Extract content type from Base64 header
        var contentType = incUrl.substring(incUrl.indexOf(':') + 1, incUrl.indexOf(';'));

        // Decode the Base64 string into bytes
        var base64Bytes = GlideStringUtil.base64DecodeAsBytes(base64String);

        // Get the incident record
        var rec = new GlideRecord('incident');
        rec.get(incSysId);

        // Write the attachment with the correct content type
        var attachment = new GlideSysAttachment();
        var fileName = "newImg.png"; // You can dynamically set this if needed
        var agr = attachment.write(rec, fileName, contentType, base64Bytes);
        
        return attachment.sys_id;
    },
    type: 'AttachRec'
});

 

I hope my answer helps you to resolve your issue, if yes please mark my answer correct and helpful.

thank you

rajesh