Any get GlideSysAttachment.write to work for binary files while in a scope?

reedwowens
Giga Expert

I have been bashing my head against this and can't believe I'm the only one to run into this.   Hope it's just I'm doing something wrong.

To test problem I created a simple form with an encoded data field and a UI action:

var sa = new GlideSysAttachment();

var binData = gs.base64Decode(current.encoded_contents);

sa.write(current, "Test.pdf", "application/pdf",binData);

action.setRedirectURL(current);

You would think this works... NOPE.   I get an attachment.   It thinks it's PDF and it's about 20% larger than the original.   All the ascii text is correct in it but the binary is messed up.

I've tested the encode/decode -> mid server (did it in java) -> server... All working but this write function.

Developer site has only the description of the call and it's function.   No examples.   From my "working with it' it does take the text and write it to the attachment..

Any help/insight would be greatly appreciated!

Just tried another obvious thing.   Added a PDF as an attachment to this record and ran the following UI Action:

var gr = new GlideRecord("sys_attachment");

gr.addQuery( "table_name", "=", "x_uxs_test_error_table");

gr.addQuery( "table_sys_id", "=" , current.sys_id + "");

gr.query();

if (gr.next()) {

  gs.addInfoMessage("Getting attachment");

  var sa = new GlideSysAttachment();

  var contents= sa.getContentBase64(gr);

  gs.addInfoMessage("Got Contents: ");

  current.encoded_contents = contents;

  current.update();

  sa.write(current, "newOrig.pdf","application/pdf", gs.base64Decode(contents));

}

action.setRedirectURL(current);

I get the infomessages.   I get the data in base64 encoded on the form.   I get a second "newOrig.pdf" and it's wrong!

1 ACCEPTED SOLUTION

reedwowens
Giga Expert

I've got this in as a bug to ServiceNow.   They are aware of the issue and looking at fixing it in a future release.   For now GlideSysAttachment().write() works only for text files and is not binary safe.   Problem


View solution in original post

19 REPLIES 19

reedwowens
Giga Expert

Here is the code that we use to work around this issue.   I've been told that the issue is fixed in Istanbul, but I haven't tested to see.   Since we will be releases prior to Istanbul for awhile, we will use this work-a-round.



This is the contents of a Script Include: CreateAttachment



var CreateAttachment = Class.create();
CreateAttachment.prototype = {
initialize: function(record,name,contentType, data) {
    var attCreator = new GlideRecord('ecc_queue');
    attCreator.agent = "AttachmentCreator";
    attCreator.topic = "AttachmentCreator";
    attCreator.name =   name + ":" + contentType;
    attCreator.source = record.getTableName()+":"+record.sys_id;
    attCreator.payload = data;
    attCreator.insert();
},

type: 'CreateAttachment'
};



In your code you can now use:



// Save an attachment on the current record


// data contains the Binary image in Base64



new CreateAttachment(current,"MyPhoto.jpg","image/jpeg",data);


This was the missing part for my solution as well. Thank you so much! For the communities sake, if anyone is looking for an HTML--> PDF converter, pdfkit for Python (give that MidServer a workout) is exceptional out of all the ones I've tried so far. Unfortunately the version of itext ServiceNow is using is quite old, otherwise it would be a viable solution.


hi Reed Owens,


I have tried using your solution to add an attachment to a record using ecc_queue, but i am unable to add an attachment.


When i went on to check whats the issue is, the record gets created in ecc queue but in Payload i am getting a message " Attachment Creation Failed "


can you help me solving whats the issue is...


any idea how is posible to replace attachment with same name in case if its exist already?

Kvark,

 

We do exactly this.  Just look for attachments with same name against your table in the sys_attachment table and use GlideSysAttachment.deleteAttachment() to remove;   You can't just replace the old contents with the new.   The attachments are stored in muliple record (chunks).  

Here is a code block that removes the attachments this:

removeAttachments(current, name) {
        var sa = new GlideSysAttachment();
        var gr = new GlideRecord('sys_attachment');
        var tableName = current.getTableName();
        var sys_id = current.sys_id + "";
        gr.addQuery('file_name', '=', name);
        gr.addQuery('table_name', '=', tableName);
        gr.addQuery('table_sys_id', '=', sys_id);
        gr.query();

        while (gr.next()) {
            sa.deleteAttachment(gr.sys_id + "");
        }
}