<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: Issue with base64 image after conversion in Developer forum</title>
    <link>https://www.servicenow.com/community/developer-forum/issue-with-base64-image-after-conversion/m-p/3039770#M1144216</link>
    <description>&lt;P&gt;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.&lt;/P&gt;</description>
    <pubDate>Mon, 09 Sep 2024 13:23:34 GMT</pubDate>
    <dc:creator>Daniel Madsen</dc:creator>
    <dc:date>2024-09-09T13:23:34Z</dc:date>
    <item>
      <title>Issue with base64 image after conversion</title>
      <link>https://www.servicenow.com/community/developer-forum/issue-with-base64-image-after-conversion/m-p/3039704#M1144193</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Hari1_0-1725882943644.png" style="width: 400px;"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/384492iA714AA35831A445C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Hari1_0-1725882943644.png" alt="Hari1_0-1725882943644.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is the code that i am using:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Client Script - OnChange:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;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);
}&lt;/LI-CODE&gt;&lt;P&gt;Script Include:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;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'
});&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 09 Sep 2024 11:56:24 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/issue-with-base64-image-after-conversion/m-p/3039704#M1144193</guid>
      <dc:creator>Hari1</dc:creator>
      <dc:date>2024-09-09T11:56:24Z</dc:date>
    </item>
    <item>
      <title>Re: Issue with base64 image after conversion</title>
      <link>https://www.servicenow.com/community/developer-forum/issue-with-base64-image-after-conversion/m-p/3039714#M1144196</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;SPAN&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/2533"&gt;@Hari1&lt;/a&gt;&amp;nbsp;,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Can you please elaborate more on the data URL, what exactly you are getting there .&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Anchal Jha&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Sep 2024 12:25:15 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/issue-with-base64-image-after-conversion/m-p/3039714#M1144196</guid>
      <dc:creator>anchal1234</dc:creator>
      <dc:date>2024-09-09T12:25:15Z</dc:date>
    </item>
    <item>
      <title>Re: Issue with base64 image after conversion</title>
      <link>https://www.servicenow.com/community/developer-forum/issue-with-base64-image-after-conversion/m-p/3039770#M1144216</link>
      <description>&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Sep 2024 13:23:34 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/issue-with-base64-image-after-conversion/m-p/3039770#M1144216</guid>
      <dc:creator>Daniel Madsen</dc:creator>
      <dc:date>2024-09-09T13:23:34Z</dc:date>
    </item>
    <item>
      <title>Re: Issue with base64 image after conversion</title>
      <link>https://www.servicenow.com/community/developer-forum/issue-with-base64-image-after-conversion/m-p/3039860#M1144247</link>
      <description>&lt;P&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/234414"&gt;@Daniel Madsen&lt;/a&gt;&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/508919"&gt;@anchal1234&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have added the dataUrl string text check the base64 url text that i am passing. Please find the attached text file.&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Mon, 09 Sep 2024 14:25:30 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/issue-with-base64-image-after-conversion/m-p/3039860#M1144247</guid>
      <dc:creator>Hari1</dc:creator>
      <dc:date>2024-09-09T14:25:30Z</dc:date>
    </item>
    <item>
      <title>Re: Issue with base64 image after conversion</title>
      <link>https://www.servicenow.com/community/developer-forum/issue-with-base64-image-after-conversion/m-p/3039877#M1144251</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/2533"&gt;@Hari1&lt;/a&gt;&lt;/P&gt;&lt;P class=""&gt;&lt;SPAN class=""&gt;In the &lt;/SPAN&gt;&lt;SPAN class=""&gt;GlideSysAttachment.write()&lt;/SPAN&gt;&lt;SPAN class=""&gt; method, you're passing an empty &lt;/SPAN&gt;&lt;SPAN class=""&gt;contentType&lt;/SPAN&gt;&lt;SPAN class=""&gt; variable. This needs to be the correct MIME type (e.g., &lt;/SPAN&gt;&lt;SPAN class=""&gt;image/png&lt;/SPAN&gt;&lt;SPAN class=""&gt;, &lt;/SPAN&gt;&lt;SPAN class=""&gt;image/jpeg&lt;/SPAN&gt;&lt;SPAN class=""&gt;) for the image to display properly.&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=""&gt;&amp;nbsp;&lt;/P&gt;&lt;P class=""&gt;&lt;SPAN class=""&gt;Update your script as bellow and try once:&lt;/SPAN&gt;&lt;/P&gt;&lt;P class=""&gt;&lt;SPAN class=""&gt;Client script&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;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);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Script include&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;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'
});&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope my answer helps you to resolve your issue, if yes please mark my answer correct and helpful.&lt;/P&gt;&lt;P&gt;thank you&lt;/P&gt;&lt;P&gt;rajesh&lt;/P&gt;</description>
      <pubDate>Mon, 09 Sep 2024 14:41:07 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/issue-with-base64-image-after-conversion/m-p/3039877#M1144251</guid>
      <dc:creator>Rajesh Chopade1</dc:creator>
      <dc:date>2024-09-09T14:41:07Z</dc:date>
    </item>
    <item>
      <title>Re: Issue with base64 image after conversion</title>
      <link>https://www.servicenow.com/community/developer-forum/issue-with-base64-image-after-conversion/m-p/3042297#M1144845</link>
      <description>&lt;P&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/74479"&gt;@Rajesh Chopade1&lt;/a&gt;I was able to achieve the functionality of adding the screenshot for the attachment table but for some reason i see the attachments are getting added twice. The old image(previous image) and the new image both are getting added to the attachment record.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2024 14:09:47 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/issue-with-base64-image-after-conversion/m-p/3042297#M1144845</guid>
      <dc:creator>Hari1</dc:creator>
      <dc:date>2024-09-11T14:09:47Z</dc:date>
    </item>
  </channel>
</rss>

