Getting contents of an attachment
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-04-2013 01:34 PM
I need to get the contents of an attachment and have tried using the following in a background script (for testing purposes only):
var sa = new GlideSysAttachment();
var binData = sa.getBytes('incident', '892a8ba0a400f000e9ece890a93b7172');
gs.print(binData);
However the response I get back is always something like:
[B@bdbc52
Which doesn't look at all like the file I have attached. I've tried a few different file attachment types and attached them to a few different types of record.
I have also tried passing a only GlideRecord on the sys_attachment table to getBytes and I get the same answer.
Once I have the contents of the file, I'd like to modify the content and create a new attachment (on a different record) with my modified content.
It looks like all of this should be possible, but I just can't get past the garbage I get out of getBytes.
I'm on Calgary Patch 2 Hotfix 5.
Thanks in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-31-2018 04:24 AM
Hi Matthew,
This works absolutely fine for .txt and csv files. But my requirement is to read word document. When i tried i I only got something like
[B@17b8d6e
Please help me out
var gliObj= new GlideRecord('sys_attachment');
gliObj.addQuery('table_name','u_request');
gliObj.addQuery('table_sys_id','c5e67213db5a13883b8e73498c961921');
gliObj.orderByDesc('sys_updated_on');
gliObj.query();
while(gliObj._next())
{
var sa = new GlideSysAttachment();
var binData = sa.getBytes(gliObj);
strData =String(Packages.java.lang.String(binData));
}
gs.print(binData)
gs.print(strData)
Please check and let me know if you can help me out
thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-05-2015 09:46 AM
Sorry to bump an old post, however I am trying to receive approvals via SMS. Issue I am having is that iOS apparently translates SMS to MMS which is coming into ServiceNow as an attachment.
No biggie, I figured I would use the solution found in this thread:
if (body != '')
{
newBody = body.substring(body.search("RITM"), body.search("RITM") + 11);
} else {
StringUtil = GlideStringUtil;
sa = new GlideSysAttachment();
var bytesContent = sa.getBytes('sys_email', email.sys_id);
var strData = String(Packages.java.lang.String(bytesContent));
newBody = strData.substring(body.search("RITM"), body.search("RITM") + 11);
}
Problem is the above is erroring out in the inbound action with the following:
- org.mozilla.javascript.EvaluatorException: The choice of Java constructor java.lang.String matching JavaScript argument types (null) is ambiguous; candidate constructors are: (char[]), (byte[]), (java.lang.StringBuffer), (java.lang.StringBuilder), (java.lang.String) (<refname>; line 14)
(The line 14 the error is pointing to is the 'var strData' declaration line)
Any ideas what the issue is? Looks like anyone else who has found this issue is related to the Java string being input instead of the JavaScript string but I thought 'var strData = String(Packages.java.lang.String(bytesContent)); would solve this issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-05-2015 11:06 AM
Nevermind! I figured it out!
Shame on me! Will record below how I fixed it just in case.
var bytesContent = sa.getBytes('sys_email', email.sys_id);
This doesn't make sense because there is no 'email.sys_id' property. Had to GlideRecord using the following to pull the sys_id:
var getEmail = new GlideRecord('sys_email');
getEmail.addQuery('uid', email.uid);
getEmail.orderByDesc('sys_created_on');
getEmail.query();
getEmail.next();
emailSysID = getEmail.sys_id;
The little things!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2017 09:35 PM
//Response stream for writing to
var out = new Packages.java.util.zip.ZipOutputStream(g_response.getOutputStream());
//[Start: Loop through attachments here]
//Read in attachment
//Replaces standard method which is limited to 5mb
//new GlideSysAttachment(grAttachment.sys_id).getBytes();
var gsa = GlideSysAttachmentInputStream(grAttachment.sys_id.toString());
var baos = new Packages.java.io.ByteArrayOutputStream();
gsa.writeTo(baos);
baos.close();
// Add ZIP entry to output stream.
out.putNextEntry(new Packages.java.util.zip.ZipEntry('/' + DirectoryName + '/' + grAttachment.fileName.toString()));
out.write(baos.toByteArray(), 0, baos.toByteArray().length);
out.closeEntry();
//[End: Loop through attachments here]
// Complete the ZIP file
out.close();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-22-2018 07:28 AM
edit -----------------
Of course, after almost 2 days of searching, I found a valid solution after my "need help" post... So I share with you my solution (apparently GlideStringUtil and other string conversion scripts are not working correctly on Byte array) also inspired by Ben's solution. Include this in your JS class (or script include) and call var payload = new name0fScriptInclude().getPayloadFileInBase64(source record sys_id, name of the file) to get the base64 payload :
getPayloadFileInBase64 : function(sourceGrSysId, fileName){
//returns payload of a file in base 64
//sourceGrSysId is the string sys_id of the source record : incident, mail, etc
//fileName is the string of the file to retreive content
//function returns base64 payload
//constructor.attachmentGr is the sys_attachment GlideRecord object
var attGr = this.getAttGr(sourceGrSysId, fileName);
if(attGr.next()) {
this.attachmentGr = attGr;
var gsa = GlideSysAttachmentInputStream(attGr.getValue("sys_id"));
var baos = new Packages.java.io.ByteArrayOutputStream();
gsa.writeTo(baos);
baos.close();
var payload = GlideBase64.encode(baos.toByteArray());
return payload;
}
return "";
},
getAttGr: function(sysId, fileName){
var attGr = new GlideRecord("sys_attachment");
attGr.addQuery("table_sys_id", sysId);
attGr.addQuery("file_name", fileName);
attGr.query();
return attGr;
},
-------------------------
Hi,
As it seems that all of you in this thread worked on manupilating bytes form attachment, I try to ask you a question related to this. I tried several ways to get back binaries of attachments (input stream, get, getBytes, GlideTextReader and your way). All of them are working for text files but they are not working for binaries files.
I'm working on attached images, I need to get back the binary content of the image to include it on email after conversion to base 64 (in the body of the email). But the string containing the binary content seems to be slighty different from the original. It seems to me, but I'm not sure, that Ascii caracters are correctly strored in the string but not the other ones. This cause that the content of the image is totally unusefull.
Do you know a way to store the binary correctly and encode it to base64 properly (scoped glide attachment seems to have such feature but I work in the global scope to manipulate emails) ?
Tks !