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
‎08-25-2017 05:10 AM
Hi,
Its not working for me ,any reason.
------------------------------------------
Below is the code I'm using.
-----------------------------------------
StringUtil = GlideStringUtil;
sa = new GlideSysAttachment();
var bytesContent = sa.getBytes("incident", "85071a1347c12200e0ef563dbb9a71c1");
gs.print("Your thing looks like: " + bytesContent);
var strData = String(Packages.java.lang.String(bytesContent));
gs.print(strData);
it is giving some encrypted value containing many structures.
-----------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-28-2017 12:50 AM
Hi Andrew what would you do if the content is an image StringUtil wouldn't work in that case. Any recommendations?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-28-2017 10:18 AM
I'm not clear on what you are trying to do. Can you explain more? I'll take a guess that you want to display an image from an attachment. In that case, you would need to base64 encode the binary data from the image file. In my test instance, I attached a PNG file to an Incident record. I then created the following UI Page:
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<g:evaluate var="jvar_b64_image">
var byteContent = new GlideSysAttachment().getBytes("incident", "6a8bb6054f5532000b3d786f0310c7e2");
GlideStringUtil.base64Encode(byteContent);
</g:evaluate>
<img src="data:image/png;base64,${jvar_b64_image}" />
</j:jelly>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-28-2017 05:38 PM
Thanks Andrew I will explain. I am generating a word document from a record. I need to pick specific attachments to that record which are images. I have the attachment sys_ids of those attachments with me.
Currently I resolved it a round about way by calling the servicenow Rest API attachment service then putting that image into db_image and then using the image url in my word doc.
But I feel I should be directly able to get specific attachment on the record and then display in word doc. My background code below:
getdata();
function getdata() {
var base = '';
var quote = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'><xml><w:WordDocument><w:View></w:View><w:Zoom>100</w:Zoom><w:DoNotOptimizeForBrowser/></w:WordDocument></xml>";
var gr = new GlideRecord('sys_attachment_doc');
gr.addQuery('sys_attachment.sys_id', '740ce90c4fad4f404acefaf11310c713');
gr.orderBy('position');
gr.query();
var len = gr.getRowCount().toString().length;
while(gr.next()) {
base = base + GlideStringUtil.base64Decode(gr.data);
}
//I got stuck on g unzip hence thought i could get the attachment bytes as per your style by using quote = quote + "<tr><td >"+ "<img src='data:image/png;base64,'" + b64_image + "/>" + "</td></tr>"; but that didn't work for me.
quote = quote + "<tr><td >"+ img +"</td></tr>"
//attach word doc to image file
var curObj = new GlideRecord('db_image');
curObj.get('7b33fd004fed4f404acefaf11310c7fe');
var pdfObj = GlideSysAttachment();
pdfObj.write(curObj,"Draft SOW.doc", "application/msword",quote);
gs.print(len);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-09-2015 04:06 AM
Hi everyone,
These answers have been very helpful to me.
However, I'm trying to write this code in Jelly, and when I get to the Package.java.. call, it doesn't seem to work properly.
Here is a snippet of my Jelly/XML/JavaScript code:
<table>
<g:evaluate>
!var gr = new GlideRecord('sys_attachmen !t');
gr.addQuery('table_name', 'ZZ_YYsys_user');
gr.addQuery('sys_updated_by', gs.getUserName());
gr.orderBy('table_sys_id');
gr.orderByDesc('sys_created_on');
gr.query();
</g:evaluate>
<j:while test="${gr.next()}">
<tr>
<td>
<j:set var="jvar_attachment" value="${new GlideSysAttachment()}" />
<j:set var="jvar_bytes" value="${jvar_attachment.getBytes('ZZ_YYsys_user', 'ff4ca4820fb40600b301c4dce1050ed6')}" />
<j:set var="jvar_photo" value="${jvar_Packages.java.lang.String(${jvar_bytes})}" />
${jvar_photo}
</td>
<td> Updated on: ${gr.sys_updated_on} <hr /> </td>
</tr>
</j:while>
</table>
The code works perfectly until it gets to the line: "<j:set var="jvar_photo" value="${jvar_Packages.java.lang.String(${jvar_bytes})}" />"
If I'm writing it wrong, could anyone please suggest the proper way to write it?
My goal is to get the bytes/binary of an attachment.
Thank you for any help in advance.