从RestFul Java Spring后端下载角文件 [英] Angular file download from RestFul Java Spring backend

查看:205
本文介绍了从RestFul Java Spring后端下载角文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种情况,我想从RestFul Java后端下载大文件。
Java后端将分块传输文件,请参见以下代码:

I have a situation where I want to download a large file from my RestFul java backend. The java backend will stream the file in chunks see following code:

    String filePath = this.workProgressService.getFilePath(entityId);
    Path path = Paths.get(filePath);
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String mimeType = fileNameMap.getContentTypeFor(path.getFileName().toString());
    if (mimeType == null) {
        mimeType = "application/octet-stream";
    }
    response.setContentType(mimeType);
    response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", path.getFileName().toString()));
    FileCopyUtils.copy(Files.newInputStream(path), response.getOutputStream());

服务运行正常。 FE,当我在浏览器中进入我的恢复点时 http:// localhost:8080 / download 一个文件窗口弹出,我可以下载文件。

The service is working fine. F.E. when I go in the browser to my restendpoint http://localhost:8080/download a file window popup and I can download the file.

但是当我用以下代码从角度进行操作时:

But when I do this from angular with the following code:

        const options = new RequestOptions({responseType: ResponseContentType.Blob});
        return this.authHttp.get(this.configurationService.getApiURL() +  '/filedownload', options)
        .map(response => {
            console.log(response.blob());
        });

当文件完全下载完后,blob的控制台日志就会触发。如果我在Chrome的开发者控制台中看到了请求,则可以看到它先下载了整个文件,然后才记录响应。这样,只有在响应出现之前,才会打开文件保存对话框。

The console log of the blob is triggered at the end when the file is completely downloaded. If I watch the request in the developer console of Chrome I can see it download the whole file first before it will log it's response. This way the file save dialog is not opened untill the response is there.

问题是我不能只使用href指向Rest端点的位置,因为我需要在GET请求中发送授权标头。

The problem is I can't just use a href to the location of the Rest endpoint because I need to send an authorization header in the GET request.

我该如何解决该问题,即用户可以直接从后端通过对话框下载文件而不必等到浏览器完全下载了文件?

How can I solve this that the user can download the file with a dialog from the back-end directly and not have to wait till browser has completely downloaded the file?

推荐答案

问题已解决。
angular的GET请求将在整个get请求完成时触发fileaver对话框。当下载大文件时,这是不理想的,因为它将下载到浏览器的内存中。

The problem is solved. The GET request of angular will trigger the filesaver dialog when the whole get request is completed. When downloading a large file this is not ideal because it will be downloaded in the browsers memory.

因为我必须使用通常位于标头中的授权令牌创建解决方法。

Because I have to use an authorization token which normally is in the header I had to create a workaround.

解决方法的确是使用带有RESTful端点的window.location.href进行下载。但是在转到该端点之前,将调用宁静的后端以创建一个有效的短暂令牌,该令牌可有效下载文件。
此令牌附加在location.href的URL上。

The workaround is indeed using the window.location.href with the RESTful endpoint to download. But before going to this endpoint the restful backend is called to create a short to live token which is valid to download a file. This token is appended to the URL for the location.href.

现在,浏览器处理文件对话框,将文件保存在计算机上。

Now the browser handles the file dialog to save the file on the computer.

这篇关于从RestFul Java Spring后端下载角文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆