你能帮助使用一些示例java代码来使用ServiceNow geneva的REST API执行文件(多种内容类型,如csv,jpg,xls,xlsx)附件吗?
提前感谢您的帮助。
答案 0 :(得分:2)
我在ContentPage中创建了以下html代码,以便在ServiceNow Geneva版本中上传文件。我测试了它,它适用于TXT,DOCX,PDF,JPEG,GIF,ICONS等。这也可用于UI页面。出于安全考虑,我建议你做一些修改。
创建 REST消息并通过脚本包含进行调用,例如 该代码不会向最终用户公开。
<script>
function uploadFileTest()
{
var obj=document.getElementById('fileUpload');
var file=obj.files[0];
var fileName=obj.files[0].name;
var encodedFile='';
//Encode file
var reader = new FileReader();
reader.onload = function(readerEvt)
{
var binaryString = readerEvt.target.result;
encodedFile= btoa(binaryString);
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function ()
{
if (this.readyState === 4) {
console.log(this.responseText);
}
});
// xhr.open("POST","https://XXXXX.service-now.com/api/now/table/ecc_queue");
xhr.open("POST","https://XXXXX.service-now.com/ecc_queue.do?JSONv2");
var data='{"agent":"AttachmentCreator","topic":"AttachmentCreator","name":"'+fileName+'","source":"u_itinerary_attchements:"'+sysid+'","payload":"'+encodedFile+'"}';
xhr.setRequestHeader("authorization", "Basic anBhdmFuOmpwYXZhbg==");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("cache-control", "no-cache");
xhr.send(data);
};
reader.readAsBinaryString(file);
}
</script>
<input type='text' id='response' />
</form>
</body>
</html>