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 08:02 AM
It's about 50K and it's a ZIP file that contains a .docx file.