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
‎11-01-2019 08:06 AM
Hello NGO,
Please refer to this LINK. It is very helpful.
If my answer helped you in any way, please then mark it as helpful. If this solved your case please mark it as correct answer. Thank You.
Best regards,
Ramesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-09-2020 12:55 PM
Hello,
Sorry to go back to this late topic but I would like to know if you did find a way to resolve your issue ?
I do have the same one.
Cheers,
Ben
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-18-2020 02:22 AM
Ditto. Did you manage to solve it?
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-18-2020 02:22 AM
Hi -
I'm also wondering if you ever found a solution!
I'm working on this right now...
Much thanks!