java - Download file from rest webservices spring -
i'm trying download file calling rest webservices. i'm using spring + jersey web services , angular 2 front. when clink on front, webservices file the window download not shown.
my rest api :
@post @path("/download") @apioperation(value = "download") @produces(mediatype.application_octet_stream) public response downloadfile(@apiparam(value = "file", required = true) string filepath) { file file = new file("/webapps/pdf/upload/msg/1/gest.png"); response.responsebuilder response = response.ok((object) file); try { string contenttype = files.probecontenttype(file.topath()); response.header("content-disposition", "attachment; filename="+file.getname()); response.header("content-type", contenttype); return response.build(); } catch (ioexception e) { e.printstacktrace(); } return null; }
my angular service :
downloadfile(path) { const headers = new headers({'content-type': 'application/x-www-form-urlencoded', 'accept': '*'}); const options = new requestoptions({headers: headers}); options.responsetype = responsecontenttype.blob; return this.http.post(apiurl + "msg/download", path, options) .catch(this.handleerror); }
my angular component :
downloadfile(documentpath) { this.msgservice.downloadfile(documentpath).subscribe(response => { var contenttype = response.headers('content-type'); let url = window.url.createobjecturl(new blob([response._body], {type: contenttype})); window.open(url); }); }
html :
<figure class="ui-g-12 " *ngfor="let document of msg.documents_path" (click)="downloadfile(document)"> <img [src]="selectimagebyextension(document.split('.').pop().tolowercase())" /> <figcaption>{{document.split('/').pop().tolowercase()}}</figcaption> </figure>
when click on figure
can see file gotten:
but nothing pops up. did miss ?
so solution working me use request instead of post passing filepath pathparam.
rest api :
@get @path("/download/{filepath}") @produces(mediatype.application_octet_stream) public response getdownloadfile(@pathparam("filepath") string filepath) { string path = null; byte [] barr = base64.getdecoder().decode(filepath); path = new string(barr); file file = new file(path); try { string contenttype = files.probecontenttype(file.topath()); response.responsebuilder response = response.ok((object) file); response.header("content-disposition", "attachment; filename="+file.getname()); response.header("content-type", contenttype); response.header("content-length", file.length()); return response.build(); } catch (ioexception e) { e.printstacktrace(); return response.status(status.internal_server_error).entity(e.getmessage()).build(); } }
angular service :
downloadfile(path) { const headers = new headers({'content-type': 'text/plain', 'accept': '*'}); const options = new requestoptions({headers: headers}); options.responsetype = responsecontenttype.blob; return this.http.get(apiurl + "msg/download/"+btoa(path), options) .map(res => res) .catch(this.handleerror); }
angular component :
downloadfile(documentpath) { this.msgservice.downloadfile(documentpath).subscribe(response => { let params = documentpath.split('/' ); var blob = new blob([response._body]); filesaver.saveas(blob, params[params.length-1]); }); }
Comments
Post a Comment