从响应用户发送的文件,春天REST +的jQuery用户发送、春天、文件、jQuery

2023-09-11 01:29:59 作者:风吹屁屁凉丶

我有春天的控制器是这样的:

I have Spring controller like this:

    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public byte[] getFilesInZIP(@RequestParam("filenames[]") String[] filenames){
        byte[] file = service.packFilesToZIPArchiveAndReturnAsByteArray(filenames);
        return file;
    }

而现在,当我创建前端,我想送数组,包含文件名的控制器和下载的ZIP档案,其中包含的文件,我选择。

And now when I create frontend, I want to send array containing filenames to the controller and download ZIP archive which contains files I chose.

(需要明确的是:让我们假设我要下载中心3个文件: a.txt中,b.txt和c.txt ;我使用jQuery发送Ajax请求与数组: ['a.txt中','b.txt','c.txt'] 作为阿贾克斯的数据,现在我想春天控制器来找到这些文件,将它们打包为 ZIP 存档,并将其发送档案给我的回应的byte [])

(To be clear: lets assume that I want to dowload 3 files: a.txt, b.txt and c.txt; I sent AJAX request using jquery with array: ['a.txt', 'b.txt', 'c.txt'] as ajax data. And now I want spring controller to find these files, pack them to ZIP archive and send this archive to me as byte[] in response)

下面是一个示例jQuery的code:

Here is sample jQuery code:

        var tab = ['a.txt', 'b.txt', 'c.txt'];
        jQuery.ajax({
            url: "http://localhost:8080/download",
            type: "GET",
            data: {
                'filenames': tab
            },
            error : function(jqXHR, textStatus, errorThrown) {
                alert('problem');
            }
        });

不过,我不知道如何处理这个响应发送此存档ZIP文件给用户。当我试图做到这一点使用的HTML一切的还不错。

But I have no idea how to handle this response to send this archive ZIP file to the user. When I was trying to do this using 's in HTML everything was okay.

我已经做了研究,但没有发现任何有用的。有一些jQuery插件,下载的文件,而是来自像网址:本地主机/ file.txt的;我想,我说上我的文件被下载并首先,额外送数组名到控制器。

I have done a research but found nothing helpful. There was some jQuery plugin which downloaded the file, but from URL like: localhost/file.txt ; I want my file to be downloaded as I said upper and first, additionally send array with filenames to the controller.

推荐答案

我建议你使用这个JS插件 https://github.com/johnculviner/jquery.fileDownload 下载而不是直接使用$阿贾克斯的文件。你可以找到那里所有的官方文件。

I recommend you to use this JS plugin https://github.com/johnculviner/jquery.fileDownload to download the file instead using $.ajax directly. You can find all the official documentation there.

此外,在你的控制器,你必须把你的HttpServletResponse并写入OutputStream的字节[],你必须把一个cookie中奉劝JS插件的响应(因为它需要它)

Also, in your controller, you have to take your HttpServletResponse and write the byte[] in the outputstream, and you have to put a cookie in the response to advise the JS plugin (because it needs it)

例如:

@RequestMapping(value = "/download", method = RequestMethod.GET)
    public void getFilesInZIP(@RequestParam("filenames[]") String[] filenames, HttpServletResponse httpServletResponse){
        byte[] file = service.packFilesToZIPArchiveAndReturnAsByteArray(filenames);

        Cookie cookie = new Cookie("fileDownload", "true");
        cookie.setPath("/");

        httpServletResponse.addCookie(cookie);
        httpServletResponse.setContentType("application/zip");
        httpServletResponse.setHeader("Content-Disposition", "attachment;filename=files.zip");
        httpServletResponse.getOutputStream().write(file);

    }