javascript - Display pdf content in a tab in firefox -
i use spring boot, jquery , itext.
when user click on button, pdf file genereated.
@getmapping(value = "/members/{memberid}/contract/{contractid}/generalcontracts", produces = "application/pdf") public responseentity<byte[]> getmembercontract(@pathvariable("memberid") long memberid, @pathvariable("contractid") long contractid) throws ioexception { byte[] content = reportservice.generatemembercontractreport(contractid); return preparereport(content); } private responseentity<byte[]> preparereport(byte[] content) throws ioexception { httpheaders headers = new httpheaders(); headers.setcontenttype(mediatype.parsemediatype("application/pdf")); string filename = "report.pdf"; headers.setcontentdispositionformdata(filename, filename); headers.setcachecontrol("must-revalidate, post-check=0, pre-check=0"); responseentity<byte[]> response = new responseentity<>(content, headers, httpstatus.ok); return response; }
a tab open , file displayed.
that code work chrome not firefox. firefox, it's tab open , close.
<button id="memberprintreport" type="button" class="btn btn-primary">imprimer</button> $("#memberprintreport").on('click', function (e) { tryit(gethostname() + "/members/" + memberid + "/contract/" + contractid + "/generalcontracts"); } function tryit(urlserver) { var win = window.open('_blank'); downloadfile(urlserver, function (blob) { var url = url.createobjecturl(blob); win.location = url; }); } function downloadfile(url, success) { var xhr = new xmlhttprequest(); xhr.open('get', url, true); xhr.setrequestheader("authorization", "basic " + $.cookie('authorization')); xhr.responsetype = "blob"; xhr.onreadystatechange = function () { if (xhr.readystate == 4) { if (success) success(xhr.response); } }; xhr.send(null); } function tryit(urlserver) { downloadfile(urlserver, function (blob) { var url = url.createobjecturl(blob); window.open(url, '_blank'); }); }
try updating line
headers.setcontentdispositionformdata(filename, filename);
to this
headers.setcontentdispositionformdata("inline", filename);
if not work try this:
headers.add("content-disposition", "inline;filename=" + filename)
Comments
Post a Comment