Load File attachment through widget servicenow portal via public page

ngo
Kilo Contributor

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',

url: 'https://xxxxxxx.service-now.com/api/now/attachment/file?table_name=sn_customerservice_case&table_sys...,

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

5 REPLIES 5

+1