Angularjs REST检索PDF

时间:2016-11-29 17:39:07

标签: java angularjs rest pdf

我正在尝试从我的后端应用程序中检索PDF文件,但我没有得到它。

看起来我没有进入我的Java REST。

修改 它现在正在工作!

JAVA REST

@RequestMapping(method = RequestMethod.GET, value = "/comprovanteInscricao/{id}")
public void get(@PathVariable String id, HttpServletResponse response) throws IOException, NumberFormatException, AplicacaoException {
    ComprovanteInscricao comprovante = iComprovanteInscricaoManager.pesquisarPeloId(Long.parseLong(id));
    byte[] arquivo = comprovante.getArquivo();

    response.setHeader("X-Frame-Options", "SAMEORIGIN");
    if (arquivo != null) {
        OutputStream outputStream = null;

        try {
            outputStream = response.getOutputStream();

            response.setContentType(PDF_MIME_TYPE);
            response.setHeader("Content-Disposition", "inline; filename=" + id + EXTENSAO_PDF);
            response.setHeader("Content-Length", String.valueOf(arquivo.length));

            IOUtils.write(arquivo, outputStream);
        } finally {
            IOUtils.closeQuietly(outputStream);
        }
    } else {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
    }
}

} Angularjs服务

angular.module('concursosApp').factory('ComprovanteInscricao', function($http, API_URL) {

return {
    downloadPdf : function(id) {

        return $http.get(API_URL + 'api/comprovanteInscricao/' + id, {
            responseType : 'arraybuffer'
        }).then(function(response) {

            return response;
        });
    }
};

}); Angularjs控制器

$scope.imprimirComprovante = function (id) {
        ComprovanteInscricao.downloadPdf(id).then(function (result) {
            var file = new Blob([result.data], {type: 'application/pdf'});
            var fileURL = window.URL.createObjectURL(file);
            window.open(fileURL);
        });
    };

感谢提前!!

0 个答案:

没有答案