Unable to add attachment to task with Activit REST API
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-18-2015 11:45 PM
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.
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.
Labels:
- Labels:
-
Archive
4 REPLIES 4
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-19-2015 12:01 AM
[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.
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.

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2015 02:42 PM
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,
It looks to me like the request is not correct.
Best regards,
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-21-2015 11:37 PM
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?
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-22-2015 12:21 AM
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!
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!
