Upload Excel file in
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2024 11:53 PM
Hello ,
I am trying to upload an excel file as an attachment from a UI Page script and then use ajax call to a script include which will store that data as an attachment. My code is currently creating an attachment- in sys_attachment table but the data I sent over seems corrupted and while downloading this attachment file seems corrupted . Here is code snippets for both client script and server include script:
UI page html :-
<?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_incNumber" expression="RP.getWindowProperties().get('incNumber')"/>
<div>
<input type="hidden" id="incNumberInput" value="${jvar_incNumber}"></input>
<h2>Upload File</h2>
<input type="file" id="fileInput"></input>
<br/>
<button onclick="submitForm()"> Submit logs</button>
</div>
</j:jelly>
UI page client script -
function submitForm() {
var fileInput = document.getElementById('fileInput').files[0];
var currentRecordInc = document.getElementById('incNumberInput').value;
if (!fileInput) {
alert('Please select a file.');
return;
}
var fileName = fileInput.name; // Get the file name
var fileType = fileInput.type;
var reader = new FileReader();
reader.onload = function(event) {
var fileContent = event.target.result; // Get the file content
uploadFile(fileName, fileContent, currentRecordInc, fileType); // Call the function to upload the file
};
reader.readAsArrayBuffer(fileInput);
}
function uploadFile(fileName , fileContent,currentRecordInc,fileType){
var ga = new GlideAjax('ExcelAttachmentParser'); // Name of Script Include
ga.addParam('sysparm_name', 'uploadAttachment'); // Method of Script Include
ga.addParam('sysparm_file_name', fileName); // File name parameter
ga.addParam('sysparm_file_content', fileContent); // File content parameter
ga.addParam('sysparm_current_record_inc', currentRecordInc); // Pass currentRecordSysId as a parameter
ga.addParam('sysparm_file_Type' , fileType);
ga.getXMLAnswer(function(response) {
alert(response); // Handle the response
});
}
script include code : -
var ExcelAttachmentParser = Class.create();
ExcelAttachmentParser.prototype = Object.extendsObject(AbstractAjaxProcessor, {
uploadAttachment: function() {
var fileName = this.getParameter('sysparm_file_name');
var fileContent = this.getParameter('sysparm_file_content');
var currentRecordSysInc = this.getParameter('sysparm_current_record_inc');
var contentType = this.getParameter('sysparm_file_Type');
// Fetch the incident record
var incGr = new GlideRecord('incident');
if (incGr.get(currentRecordSysInc)) {
// Create the attachment record
var attachment = new GlideSysAttachment();
var attachmentSysId = attachment.write(incGr, fileName, contentType, fileContent);
if (attachmentSysId) {
gs.info('Attachment created successfully with sys_id: ' + attachmentSysId);
return attachmentSysId; // Return the sys_id of the created attachment
} else {
gs.error('Failed to create attachment.');
return null; // Return null to indicate failure
}
} else {
gs.error('Incident record not found with sys_id: ' + currentRecordSysInc);
return null; // Return null to indicate failure
}
},
type: 'ExcelAttachmentParser'
});
1 REPLY 1
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-29-2024 12:14 AM
Hi @vinitaverma ,
I can suggest 2 pointers to check :
1. Check if the type of file is allowed in sys_pro
2. Try decode attachement on download.
Ref:
☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....