cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to add attachment to task with Activit REST API

hguser
Champ in-the-making
Champ in-the-making
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.
4 REPLIES 4

hguser
Champ in-the-making
Champ in-the-making
[Adding]

Also I checked with an external url and it works fine, So this has to be some issue with getting the binary content of the file, or the ajax request content.

trademak
Star Contributor
Star Contributor
Why are you using JSON.stringify to sent a file body to the REST service?
It looks to me like the request is not correct.

Best regards,

hguser
Champ in-the-making
Champ in-the-making
Thanks for the response Tijs.  Well in order to pass the name value of the file, don't we need to enode it as JSON?

hguser
Champ in-the-making
Champ in-the-making
Hi,

Got it solved, Instead of jquery, I used a form on encType multipart/form-data and sent it as a POST request. Thanks for the help!
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.