Having trouble with base64
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2019 07:35 AM
Hello folks,
I'd be grateful if someone could help me figure out a problem I am having. I've got code to grab an attachment from a request item, base64 encode it, send it to a MID server using the ECC queue, base64 decode it, then write it to a file. The problem is that the resulting file is almost twice as large as the original file. Examining it in a text editor shows extensive corruption. Thank you in advance for your time and help.
The code works in two parts. The first is the script include that I call from my workflow. It lives in a scoped app:
grSysAtt = new GlideRecord('sys_attachment');
grSysAtt.addQuery('table_name', table);
grSysAtt.addQuery('table_sys_id', gr.sys_id);
grSysAtt.query();
while (grSysAtt.next())
{
is_match = grSysAtt.file_name.match(re);
if (is_match)
{
sa = new GlideSysAttachment();
attachmentContents = sa.getContent(grSysAtt);
encData = gs.base64Encode(attachmentContents);
jspr = new global.JavascriptProbe(this.mid);
jspr.setName('CopyAttachmentToMidServer/' + gr.number);
jspr.setJavascript("var attr = new SaveAttachment(); attr.execute();");
jspr.addParameter("encodedData", encData);
jspr.addParameter("directory", this.dir + gr.number);
jspr.addParameter("file_name", grSysAtt.file_name);
jspr.create();
result.push(grSysAtt.file_name.toString());
}
}
The second part is a MID server script include that uses a Java call to decode the stream and write it to a file:
SaveAttachment.prototype.execute = function ()
{
'use strict';
var result = false;
try
{
new Packages.java.io.File( this.dir ).mkdirs();
var fos = new Packages.java.io.FileOutputStream(this.dir +"/"+ this.file_name);
var str = this.Encoded_Data;
var s = new Packages.java.lang.String(str);
var strContent = new Packages.com.glide.util.Base64().decode(s);
fos.write(strContent);
fos.close();
result = true;
}
catch(err)
{
result = err;
}
return result;
};
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2019 07:42 AM
Hi,
The first part of the code looks good i.e. getting base64encoded data for the attachment and sending it
the second part also looks good.
Is the file getting created correctly in the mid server folder? did you check the content. if the content is same then you should be good
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2019 08:00 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2019 08:21 AM
Interestingly, text appears to make it through just fine. I created a text file with normal English characters in it and a few high bit ASCII characters at the end. The normal text came through just fine and the high bit stuff was mangled. The correct version is on the right, corrupted on the left.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-28-2019 07:50 AM
What is the size of the attachment?
The documentation for the call
attachmentContents = sa.getContent(grSysAtt);
says The attachment contents as a string. Returns up to 5 MB of data.
Also what type of file attachment is it? Text,jpg, pdf?
Vinod Kumar Kachineni
Community Rising Star 2022