File upload in sharepoint list after the items created using jquery

Discussion in 'jQuery' started by jhanani, Sep 25, 2015.

  1. #1
    Hi
    I tried to add item in sharepoint list and then using the id tried to upload. The problem here is when i do debug the attachment is added to the item. If i run normally attachment has not added.




    <script>
    createTask = function () {
      
      var x = $scope.task;
      var call = addItemFields(x);
      call.done(function (data, textStatus, jqXHR) {
      var item = data.d;
      
    
      function uploadDocument() {
      var element = document.getElementById("inputFile");
      var file = element.files[0];
      var parts = element.value.split("\\");
      var fileName = parts[parts.length - 1];
    
      var reader = new FileReader();
      reader.onload = function (e) {
      addItem(e.target.result, fileName, item);
      }
      reader.onerror = function (e) {
      alert(e.target.error);
      }
      reader.readAsArrayBuffer(file);
    
    
      function addItem(buffer, fileName, item) {
      var call2 = uploadDocument(buffer, fileName, item);
      call2.done(function (data, textStatus, jqXHR) {
      alert("successfully uploaded");
      });
      call2.fail(function (jqXHR, textStatus, errorThrown) {
      failHandler(jqXHR, textStatus, errorThrown);
      });
    
      }
    
    
      
      });
      call.fail(function (jqXHR, textStatus, errorThrown) {
      failHandler(jqXHR, textStatus, errorThrown);
      });
    
      }
      
      function uploadDocument(buffer, fileName, item) {
    
      var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('MeetingTasks')/items(" + item.Id + ")/AttachmentFiles/add(FileName='" + fileName + "')";
      var call = jQuery.ajax({
      url: url,
      type: "POST",
      data: buffer,
      processData: false,
      headers: {
      Accept: "application/json;odata=verbose",
      "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
      "Content-Length": buffer.byteLength,
      "IF-MATCH": item.__metadata.etag,
      "X-Http-Method": "MERGE"
      }
      });
    
      return call;
    
      }
    
    
      function failHandler(jqXHR, textStatus, errorThrown) {
      var response = JSON.parse(jqXHR.responseText);
      var message = response ? response.error.message.value : textStatus;
      alert("Call failed. Error: " + message);
      }
    
    
    
    
      function addItemFields(item) {
      var now = new Date();
      var call = jQuery.ajax({
      url: _spPageContextInfo.webAbsoluteUrl +
      "/_api/Web/Lists/getByTitle('MeetingTasks')/Items",
      type: "POST",
      data: JSON.stringify({
      "__metadata": { type: "SP.Data.MeetingTasksListItem" },
      Title: item.taskName,
      Body: item.description, DueDate: item.dueDate
      }),
      headers: {
      Accept: "application/json;odata=verbose",
      "Content-Type": "application/json;odata=verbose",
      "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
      }
      });
    
      return call;
      }
    
      });
    </script>
    HTML:
    regards
    jhanani
     
    jhanani, Sep 25, 2015 IP