AnsweredAssumed Answered

Unable to add attachment to task with Activit REST API

Question asked by hguser on Jun 19, 2015
Latest reply on Jun 22, 2015 by hguser
Hi,

I'm trying to use rest API call to add an attachment to a task . ( attaching a file rather than giving an external url)  I'm trying to do this in a webapp, so the rest call happens as jquery ajax. Following is the js function that i'm using.


function addAttachment(id){
    var files = document.getElementById('files').files;
    var fileName = document.getElementById('files').value;
   //if no files were selected
    if (!files.length) {
        alert('Please select a file!'); //TODO
        return;
    }

    var file = files[0];
    var url = "/" + CONTEXT + "/send?req=/bpmn/runtime/tasks/" + id + "/attachments";
    var start =  0;
    var stop =  file.size - 1;
    var reader = new FileReader();

  reader.onloadend  = function(evt) {
      if (evt.target.readyState == FileReader.DONE) { // DONE == 2

          var url = "/" + CONTEXT + "/send?req=/bpmn/runtime/tasks/" + id + "/attachments";
          fileName = fileName.split(/(\\|\/)/g).pop();
          alert(evt.target.result);
          var body = {
               "name" :"test",
              "file" : evt.target.result
          };
          $.ajax({
              type: 'POST',
              mimeType: "multipart/form-data",
             contentType:"json",
              url: httpUrl + url,
              data: JSON.stringify(body),
              success: function (data,xhr) {
                  alert(data);
              },
              error: function (response) {
                alert(response.status);
              }

          });

      }

  };

  //  var blob = file.slice(start, stop + 1);
    reader.readAsBinaryString(file);


}
In Activiti user guide as it says to add the file as a binary value, I have used FileReader to get the binary content of the file.  This call goes into success but with the status code 200 instead of 201 and no response is returned. What could be the issue here?  I tried adding contentType:false as well, but it ddn't help. Appreciate some help on this.

Thanks.

Outcomes