The CreatorCon Call for Content is officially open! Get started here.

uploadAttachment return null object

User673228
Kilo Expert

Hi. I have this script : 

$scope.attachFile = function(files, $event) {
  var e = $event.currentTarget;
  console.dir("value e : " + e.value);
  if (e) {
    e.value = '';
  }
  console.log("files[0]");
  console.dir(files[0]);
		
  console.log("snAttachmentHandler before");
  console.dir(snAttachmentHandler);
	
  var sna = snAttachmentHandler.create('myUserTable', $scope.data.elementSysId);

  console.log("snAttachmentHandler after");
  console.dir(sna);

  sna.uploadAttachment(files[0]).then(function (response) {

  console.log("sna.uploadAttachment");
  console.dir(sna);
			 
  console.log("response");
  console.dir(response);
			 
     var fName = response.file_name;

     console.log("fname");
     console.log(fName);

     if (fName.indexOf('inp_') != 0) {
       fName = "inp_" + fName;
       snAttachmentHandler.renameAttachment(response.sys_id, fName).then(function (response) {
           var nah = $scope.attachmentHandler = new nowAttachmentHandler(setAttachments, function () { });
           nah.setParams('myUserTable', $scope.data.elementSysId, 1024 * 1024 * 24 /*24MB*/);
           function setAttachments(attachments, action) {
               $scope.attachments = attachments;
               $scope.data.attachmentList = attachments;
           }
           $scope.attachmentHandler.getAttachmentList();
       });
     }
   });
};

But i get an error, always the same : TypeError: Cannot read property 'indexOf' of undefined

I use the same script in another widget, and everythings work fine... Somebody can help?

 

[EDIT]

This is the console log result : 

find_real_file.png

1 ACCEPTED SOLUTION

User673228
Kilo Expert

I found the problem... I didn't have enough right on my table to allow the user to insert an attachment. So I've add the right WRITE to my table.

View solution in original post

10 REPLIES 10

Nestor Paredes
Tera Contributor

Hello,

I experienced the same error, but the cause was different:

I was reading from the attachment
The file was too big for :

 

 

GlideSysAttachment().get()

 

 

 

 
I had to use this instead:

 

 

 

// Now we need to read the file
    // we need to do this because of its size, otherwise it would crash and return null
    var attachSysID = grAttachment.getUniqueValue();
    // we create an input stream based on the attachment
    var inputStream = new GlideSysAttachment().getContentStream(attachSysID);
    // and instantiate a reader
    var reader = new GlideTextReader(inputStream);
    var ln = ' ';
    // this will have all the lines for the soon-to-be json
    var dataInTextFile;
    // adding in the lines
    while ((ln = reader.readLine()) != null) {
        dataInTextFile += ln;
    }

 

 



 

I got the idea from here:
 
Regards,
N.