1

我一直在努力让它工作一段时间。我有一个通过 GlideDialog 包含 UI 页面的 UI 操作。UI 页面只是一个包含一堆输入(文本类型)和一个文件类型的表单。单击提交按钮后,我将通过 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']);
}
});

我有相应的脚本包含它在这里调用的方法,

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);
        
    }

附件生成的记录已损坏,

在此处输入图像描述

我在这里碰壁了,不知道如何进一步进行。非常感谢这方面的任何帮助!

谢谢,

拉斯基尔

4

1 回答 1

0

我们不能直接发送文件,比如我们如何能够做到这一点等php

ServiceNow您可以使用 OOB 小部件Ticket Attachments widget-ticket-attachments

另一种方法是您需要将文件转换为 base 64 并将该数据发送到服务器脚本并使用。

这是我在一个地方使用的代码:

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

客户:

$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]);
}

服务器脚本:

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;
},
于 2020-10-07T16:01:15.087 回答