Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

Angular file download from RestFul Java Spring backend

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());

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());
        });

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...