How to script data into fields of type "File Attachment"?

Ian Elrick1
Kilo Expert

Hi

I am trying to create a service portal for learning purposes and making reasonable progress (using angular) until I need to use some of the more exotic field types. The first one is <input type="file">. I wish to store the what the user chooses in a "File Attachment" field in my table.After some work I think I have got the chosen file passing from HTML -> Client Controller -> Server script. 

However, I do not know (and cannot find) how to place that file attachment info into my column (u_myfile).

The files I have are as follows. Does anyone know if the bold bit is correct for file attachment fields ? or is there a way to do this with glidesysAttachment?

 

Thanks in advance.


<row>
<!--join date goes here-->
<div class="col-sm-8 evenrow entry">
<label for="myfile">Enter the file</label>
<input type="file" name="myfile" id="myfile" ng-file-select="addfile($files)" ng-model="data.myfile" class="form-control-file border">
</div>
</row>

Client

function($scope,$rootScope,spUtil) {
/* widget controller */
var c = this;
c.data.joindate="1999-9-9";

//c.data.myfile="test.doc";
$scope.onSubmit= function(){
c.data.myaction="save";
c.server.update();
}

$scope.addfile= function(files) {

alert("Input:"+c.data.myfile[0].name);

var input = c.data.myfile;//event.target;

var reader = new FileReader();
reader.onload = function(){
var dataURL = reader.result;
var output = document.getElementById('myfile');
output.src = dataURL;
c.data.myfile[0]=output.src;
};
reader.readAsDataURL(input[0]);
//The above gives the file contents. to be stored in the database

};

}

 

Server Script

(function() {
/* populate the 'data' object */

if(!input)
{
data.myaction="none";
data.ref="666";
data.skillset="Java and python";
//data.joindate=1387843200000;
data.joindate="1999-8-30";
data.shortdesc="rubbish";
data.myfile="";
}
//gs.addInfoMessage("server update:"+input.table);
//data.action="";
if(input && input.myaction!=undefined){
if (input.myaction=="save")
{

gs.addInfoMessage("server saving");
try{
var gr=new GlideRecord("x_excep_for_deleti_delinc");
gr.initialize();
gr.skillset=input.skillset;

gr.short_description=input.shortdesc;
gr.setValue("u_myfile",input.myfile[0]);
gs.addInfoMessage(input.myfile[0]);
gs.addInfoMessage("InputDate:"+input.joindate);

gr.insert();

}catch (e)
{gs.addInfoMessage("error:"+e);
}
}
}
})();

3 REPLIES 3

The Machine
Kilo Sage

Attachments for any record in ServiceNow are stored in the sys_attachments table.  For Service Portal, I'd recommend checking out the ticket-attachments widget that ServiceNow built. 

/nav_to.do?uri=sp_widget.do?sys_id=9ee37281d7033100a9ad1e173e24d457

Thanks

 

I had seen this and had a look through. It does not seem to refer to columns of type "file attachment" however. Also, it creates a "nowAttachmentHandler" object in the client script. I cannot find the source code or documentation about how to use this object. Have you any advice re that?

 

Thanks

 

 

M_Taimoor Jawai
Tera Expert

Any update mate? I have been doing recently a much similar task like this. Can you tell me did you get any luck on this.