I am trying (unsuccessfully so far) to attach a file (an image) to my JSON data to call a method in my webservice.
I found some posts regarding the sending of just an image but not an image as part of a json data object.
The call works if i leave "imageFile" as null.
$("#butSubmit").click(function(){
var files = document.getElementById('files').files;
var file = files[0];
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
dataType: "json",
url:"http://localhost:8080/SampleApp/api/users/add",
data: JSON.stringify({"description":"test2","title":"testest2","uploaderName":"seren","imageFile":file})
});
});
On the web service side i am calling this method:
@Path("/add")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public void addUser(Picture picture){
userService.persistPicture(picture);
}
with this constructor for the class Picture:
@JsonCreator
public Picture(
@JsonProperty("description") String description,
@JsonProperty("title") String title,
@JsonProperty("uploaderName") String uploaderName,
@JsonProperty("imageFile") byte[] imageFile) {
this.uploaderName = uploaderName;
this.title = title;
this.description = description;
this.imageFile = (byte[]) imageFile;
}
I hope you can point me in the right direction!
Thx in advance!