Can we convert Base 64 image to png image using servicenow script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2023 10:27 PM
Hello team,
I have Base 64 image string but I want to convert it to PNG format to create record in db_image table. Could somebody help in how to proceed.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2023 10:31 PM
Hi @Kajal8
Yes, it is possible to convert a Base64 encoded image to a PNG image using a script in ServiceNow. You can do this by using the GlideSystem class in ServiceNow to create a new Attachment record, and then decoding the Base64 encoded image data and writing it to the newly created Attachment.
Here is an example of how you can convert a Base64 encoded image to a PNG image using a script in ServiceNow:
// Decode the Base64 encoded image data
var imageData = new GlideString(base64EncodedImage);
var binaryData = GlideEncryption.decode(imageData);
// Create a new Attachment record
var attachment = new GlideRecord('sys_attachment');
attachment.initialize();
attachment.setValue('table_name', 'YOUR_TABLE_NAME');
attachment.setValue('table_sys_id', 'YOUR_TABLE_SYS_ID');
attachment.setValue('file_name', 'image.png');
attachment.setValue('content_type', 'image/png');
attachment.setValue('data', binaryData);
attachment.insert();
Keep in mind that, if the image is not a PNG image, you should change the 'content_type' and the 'file_name' properties accordingly.
Please mark the suggestion as helpful, if you find it useful to you or others who wants to refer similar content.
Please mark the solution as correct, if the answer provided by me has resolved your query.
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2023 11:13 PM
I tried to run this but it is giving error message "GlideString is not defined".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2023 02:21 AM
Once check this article as well:
https://www.servicenow.com/community/spm-forum/where-can-i-find-complete-documentation-about-glidest...