jquery - Send list of objects to jersey server, generate a pdf report and show in browser -
i need generate pdf using list of pojos , show in browser. i've tried 2 strategies:
make ajax request service returns filestream, , send response service shows file in browser.
a. js code:
var datatosend = json.stringify(mydata); $.ajax({ url : 'report', method : 'post', data : datatosend, contenttype : 'application/json', success : function(data) { var $form = $('<form class="hidden"></form>').attr({ action : 'mimetype', method : 'post', target : '_blank', acceptcharset : 'utf-8', enctype : 'multipart/form-data' }); var $input = $('<input class="hidden" type="file" name="file">'); $form.append($input); $("body").append($form); var fd = new formdata($form[0]); fd.append('file', file); $form.submit(); $form.remove(); } error : function(jxhr, e) { utils.showerror(jxhr, e); } })
b. these services use trying make report:
@post @path("report") @consumes(mediatype.application_json + ";charset=utf-8") public response getreport(list<pojo> data) { byte[] report = generatereport(); return response.ok(report,mimetype); } @post @path("{mimetype}") @consumes(mediatype.multipart_form_data) public response report(@formdataparam("file") inputstream file, @pathparam("mimetype") string mimetype) { try { return response.ok(file, mimetype).build(); } catch (exception e) { e.printstacktrace(); return response.ok("error", mediatype.text_plain + ";charset=utf-8") .build(); } }
each rest service works fine separatedly, problem haven't managed read in ajax. i've tried too, send byte[] or inputstream.
make form in js , send list multipart/form-data, receiving response, using js code:
var $form = $('<form class="hidden"></form>').attr({ action : 'report', method : 'post', target : '_blank', acceptcharset : 'utf-8', enctype : 'multipart/form-data' }); var $input = $('<input class="hidden" type="file" name="file">'); $form.append($input); $("body").append($form); var fd = new formdata($form[0]); fd.append('data', mydata); $form.submit(); $form.remove();
i've changed @consumes of service , set @formdataparam("data") list in service arguments, data don't arrive service. don't know else try.
Comments
Post a Comment