1

I've been trying to get this to work for a while now. I have a UI action that includes a UI page through a GlideDialog. The UI page is just a form with a bunch of input (text type) and one file type. On click of submit button I am sending the form data as well as the file attachment via glideAjax,

var issueObj = {};
var ga = new GlideAjax(glideAjax);
var name = $j_jb('#name').val();
var address = $j_jb('#address').val();
var file = $j_jb('#jira_attachment')[0].files[0];

issueObj.name = name;
issueObj.address = address;
    
var IssueObjString = JSON.stringify(issueObj);
ga.addParam('sysparm_name','createIssue');
ga.addParam('sysparm_issueObj', IssueObjString);
ga.addParam('sysparm_attachment', file);
    
var that = this;
ga.getXML(function (response) {
var responseStatus = response.responseXML.documentElement.getAttribute("answer");
var DOMData = "";
if(responseStatus) {
    that.displayMessage(jiraAlert['success-insertion']);
}
else {
    that.displayMessage(jiraAlert['error-insertion']);
}
});

I have the corresponding script include method it calls here,

createIssue: function() {
        
        var issueObj = this.getParameter("sysparm_issueObj");
        var fileAttachment = this.getParameter("sysparm_attachment");
        issueObj = JSON.parse(issueObj);
        
        var fileName = issueObj.fileAttachment.name;
        var fileType = issueObj.fileAttachment.type;*/
        var gr = new GlideRecord('sample_table');
        gr.newRecord();
        gr.name = issueObj.name;
        gr.address = issueObj.address;
        
        insertRef = gr.insert();
        var ga = new GlideSysAttachment();
        ga.write(gr, fileAttachment.name, fileAttachment.type, fileAttachment);
        
    }

The record gets generated by the attachment is corrupt,

enter image description here

I've hit a wall here, and don't know how to proceed further. Any help with this regard is highly appreciated!

Thanks,

Raskill

Raskill
  • 175
  • 13

1 Answers1

0

We cant send files directly, like how we can able to do it on php etc.

On ServiceNow you can use OOB widget Ticket Attachments widget-ticket-attachments

The alternative is you need to convert the file to base 64 and send that data to the server script and use.

Here is the code that I used in one place:

<label class="file-upload btn btn-primary">
    ${Browse for file} ... 
    <input type="file" id="fileToUpload" onchange="angular.element(this).scope().setFiles(this)"/>
</label>

Client:

$scope.setFiles = function(element) {
$scope.resumefiles = []; 
$scope.$apply(function() {
    // Turn the FileList object into an Array
    for (var i = 0; i < element.files.length; i++) {
        $scope.resumefiles.push(element.files[i]);
    }
    
    $scope.uploadResume($scope.resumefiles);
});
};

$scope.uploadResume  = function(resumefiles){
var reader = new FileReader(), base64String = "";
reader.onloadend = function () {
    base64String = reader.result.substr(reader.result.indexOf("base64,"), 
    reader.result.length-1).replace("base64,","");
    $scope.data.fileData  = base64String;
    $scope.data.fileName  = resumefiles[0].name;
    $scope.data.fileType  = resumefiles[0].type;
    $scope.data.funcName  = 'updateApplicantResume';
    c.server.update().then(function(){
        $scope.data.funcName  = '';
        $scope.resumefiles = []; 
    })
}
reader.readAsDataURL(resumefiles[0]);
}

Server script:

uploadAttachment: function(input) {
    var attachment_sys_id = '';
    var attachment = new GlideSysAttachment();
    var gr = new GlideRecordSecure(input.table);
    if (gr.get(input.applicantSysId)) {
        attachment_sys_id = attachment.writeBase64(gr, input.fileName, input.fileType, input.fileData);
    }
    return attachment_sys_id;
},
Mr world wide
  • 4,696
  • 7
  • 43
  • 97