Search code examples
javascriptjquerysharepoint-2013spservices

Upload File to Sharepoint List using SPservices


I'm having an issue with trying to upload an attachment from a file picker. When I add the file to be uploaded i just get the error.

Unable to get property '0' of undefined or null reference

and it points to the line var file = $("#SupportDoc").files[0];

here is my code. Any help would be great.

<div class="form-group" style="margin:10px;">
    <label for="exampleFormControlFile1" style="border:none;">Supporting Documentation</label>
    <input style="border:none;" type="file" class="form-control-file" id="SupportDoc">
</div>

-

function attach(){
var file = $("#SupportDoc").files[0];

getFileBuffer(file).then(function(buffer) {
  var binary = "";
  var bytes = new Uint8Array(buffer);
  var i = bytes.byteLength;
  while (i--) {
    binary = String.fromCharCode(bytes[i]) + binary;
  }
  $().SPServices({
    operation: "AddAttachment",
    listName: "Tasks",
    listItemID: taskID,
    fileName: file.name,
    attachment: btoa(binary)
  });
});
}

Solution

  • replace your line with this

    var file = document.getElementById("SupportDoc").files[0];
    

    or

    var file = $("#SupportDoc")[0].files[0]