在Spring-MVC中创建下载链接

时间:2014-12-09 15:45:32

标签: javascript spring-mvc download

我正在开发一个Spring-MVC应用程序。在应用程序中,我使用一种方法从屏幕截图创建一个pdf文件,使用前端的一些函数。现在,有一个用户点击的按钮,它通过' POST'发送图像,然后转换为PDF。问题是,因为这是一个' POST',我无法使用普通的文件下载方法来返回文件。

经过一些研究后我发现,jquery可以用来下载文件,但它需要相同的下载URL。我发布了用于创建屏幕截图PDF的代码,请告诉我如何创建下载URL。

控制器类:

 @RequestMapping(value = "/canvas/screenshot", method = RequestMethod.POST, produces = "application/pdf")
    public
    @ResponseBody
    String saveCanvasScreenshot(@RequestBody String body, Model model,HttpServletResponse response) {
 Person person = personService.getCurrentlyAuthenticatedUser();
        body = body.substring(30);
        String path = "/home/akshay/"+person.getFirstName()+".pdf";
        try {
            body = URLDecoder.decode(body, "UTF-8");
            canvasImageBytes = body.getBytes();
            canvasImageBytes = org.apache.commons.codec.binary.Base64.decodeBase64(canvasImageBytes);
            Canvas canvas = canvasService.getCanvasById(staticcanvasid);
            canvas.setCanvasimage(canvasImageBytes);
            canvasService.addCanvas(canvas, person);
            boolean b = convertimagetopdf(body);
            if(b == true){
               // And here I would like to sent the URL back as a String.
}

Javascript方法:

 $("#pdf-creator").click(function(){
           document.styleSheets[4].disabled=true;
           var screenshot = $('#main').clone();
           renderingScreenshot = true;
           setWidthsForScreenshot(screenshot);
           html2canvas($('#main'), {
                onrendered: function(canvas) {
                    var img = canvas.toDataURL("image/png");
                    img = encodeURIComponent(String(img));
                   $.ajax({
                        url: baseURL + '/canvas/screenshot',
                        type: 'POST',
                        data: img,
                        success: function(){
                            document.styleSheets[4].disabled=false;
                            $('#loading-image').show();

                        },
                            error: function(xhr, status, error){
                                $('body').append(xhr.responseText);
                                document.styleSheets[4].disabled=false;
                                $('#loading-image').show();

                            }
                    });
                }
                });
       });
       initializeGridstack();

1 个答案:

答案 0 :(得分:0)

您可以在响应中返回下载链接,然后发出GET请求。在Spring中,您可以执行此操作以从可下载链接获取文件。

@RequestMapping("/models/{fileId}.smth")
            @ResponseBody
            public HttpEntity<byte[]> getFile(@PathVariable String fileId) throws IOException {
                byte[] model = org.apache.commons.io.FileUtils.readFileToByteArray(new File( "C:/folder/" + fileId + ".smth"));
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.MULTIPART_FORM_DATA);
                headers.setContentLength(model.length);
                return new HttpEntity<byte[]>(model, headers);
            }