Load File attachment through widget servicenow portal via public page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2017 11:59 AM
Hi Folks
I want to load File attachment through widget servicenow portal via public page
The thing is that i am able to load the attachment which are type image but they are getting corrupted coz the setRequestBody is not able to read File Object which i am passing as a parameter and other type of files like excel and text does not get loaded at all
I got a code but it was written for client side for authenticated users and is working fine but in order to authenticate since it is a public page i had to call the rest api through server script
Please help
Client side -
$scope.fd = new FormData();
$scope.files.forEach(function(file){
$scope.fd.set('files', file);
c.data.method = 'POST';
//c.data.url = 'https://xxxxxxx.service-now.com/api/now/attachment/file?table_name=sn_customerservice_case&table_sys...
c.data.file.push($scope.fd.get('files'));
c.data.type.push(file.type);
c.data.filename.push(file.name);
});
c.server.update().then(function(response) {
});
};
Server Side -
for(var l=0;l<input.filename.length;l++)
{
if(input.filename[l])
{
var restMessage = new sn_ws.RESTMessageV2();
restMessage.setBasicAuth('xxxxx','xxxxx');
restMessage.setHttpMethod(input.method);
restMessage.setEndpoint('https://xxxxxx.service-now.com/api/now/attachment/file?table_name=sn_customerservice_case&table_sys_...
restMessage.setRequestHeader("Accept","application/json");
restMessage.setRequestHeader('Content-Type',input.type[l]);
//var fle = JSON.stringify(input.file[l]);
restMessage.setRequestBody(input.file[l]);
//restMessage.setRequestBodyFromAttachment(input.file[l]);
gs.log('File Content '+input.type[l]);
gs.log('File Name '+input.filename[l]);
gs.log('File Data '+fle);
//gs.addInfoMessage(input.file[l]);
var response = restMessage.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.addInfoMessage(httpStatus);
}
}
}
I am able to produce logs for multiple files
Working client code -
$scope.uploadFiles = function() {
alert("uploading attachment");
$scope.fd = new FormData();
$scope.files.forEach(function(file){
$scope.fd.set('files', file);
var request = {
method: 'POST',
data: $scope.fd.get('files'),
headers: {
'Content-Type': file.type,
'Accept':'application/json'
}
};
// SEND THE FILES.
$http(request)
.success(function (d) {
// On success code here
})
.error(function (err) {
// On error code here
});
});
}
else if i can pass some authentication parameter in the above code then it might also work
Thanks
Nitish Goel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2020 04:34 AM
+1