使用angular 7将文件流传输到文件系统

使用angular 7将文件流传输到文件系统,angular,download,filesaver.js,Angular,Download,Filesaver.js,我正在尝试使用angular 7开发一个文件下载。我正在使用HttpClient和FileSaver进行下载。我遇到的问题是,当HttpClient向服务器发出下载请求时,它会等待整个响应完成(将整个文件保存在浏览器内存中),而保存对话框仅在最后出现。我相信在大文件的情况下,将其存储在内存中会导致问题。是否有一种方法可以在收到状态OK后立即显示保存对话框,并将文件流式传输到文件系统。我还需要将授权标头与请求一起发送 我的服务器端代码: @RequestMapping(value = "/file

我正在尝试使用angular 7开发一个文件下载。我正在使用
HttpClient
FileSaver
进行下载。我遇到的问题是,当HttpClient向服务器发出下载请求时,它会等待整个响应完成(将整个文件保存在浏览器内存中),而
保存对话框仅在最后出现。我相信在大文件的情况下,将其存储在内存中会导致问题。是否有一种方法可以在收到状态OK后立即显示
保存对话框
,并将文件流式传输到文件系统。我还需要将授权标头与请求一起发送

我的服务器端代码:

@RequestMapping(value = "/file/download", method = RequestMethod.GET)
    public void downloadReport(@RequestParam("reportId") Integer reportId, HttpServletResponse response) throws IOException {
        if (null != reportId) {
            JobHandler handler = jobHandlerFactory.getJobHandler(reportId);
            InputStream inStream = handler.getReportInputStream();

            response.setContentType(handler.getContentType());
            response.setHeader("Content-Disposition", "attachment; filename=" + handler.getReportName());

            FileCopyUtils.copy(inStream, response.getOutputStream());
        }
    }
我的客户端代码(angular)


回答我自己的问题,希望它能帮助有同样问题的人。 我发现没有任何方法可以通过ajax调用直接启动流式传输到文件系统。我最后做的是创建一个名为
/token
的新端点。此端点将获取文件下载所需的参数,并创建JWT签名令牌。此令牌将用作
/download?令牌=xxx
端点的查询参数。我使用
.authorizeRequests().antMatchers(“/download”).permitAll()
绕过了spring security的这个端点。因为/download需要一个签名令牌,所以我只需要验证签名对于真实的下载请求是否有效。然后在客户端,我刚刚创建了一个动态的
标记,并触发了一个
click()
事件。 令牌提供程序:

import com.google.common.collect.ImmutableMap;
import com.vh.dashboard.dataprovider.exceptions.DataServiceException;
import com.vh.dashboard.dataprovider.exceptions.ErrorCodes;
import com.vh.dashboard.security.CredentialProvider;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.impl.TextCodec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
public class TokenProvider {

    @Value("${security.jwt.download.signing-key}")
    private String tokenSignKey;

    @Autowired
    CredentialProvider credentialProvider;

    private static int VALIDITY_MILISECONDS = 6000000;

    public String generateToken(Map claimsMap) {
        Date expiryDate = new Date(
                System.currentTimeMillis() + (VALIDITY_MILISECONDS));

        return Jwts.builder().setExpiration(expiryDate)
                .setSubject(credentialProvider.getLoginName())
                .addClaims(claimsMap).addClaims(
                        ImmutableMap
                                .of("userId", credentialProvider.getUserId()))
                .signWith(
                        SignatureAlgorithm.HS256,
                        TextCodec.BASE64.encode(tokenSignKey)).compact();
    }

    public Map getClaimsFromToken(String token) {
        try {
            return Jwts.parser()
                    .setSigningKey(TextCodec.BASE64.encode(tokenSignKey))
                    .parseClaimsJws(token).getBody();

        } catch (Exception e) {
            throw new DataServiceException(e, ErrorCodes.INTERNAL_SERVER_ERROR);
        }
    }
}
客户端代码:

 this._httpClient.post(AppUrl.DOWNLOADTOKEN, param).subscribe((response: any) => {
          const url = AppUrl.DOWNLOAD + '?token=' + response.data;
          const a = document.createElement('a');
          a.href = url;
//This download attribute will not change the route but download the file.
          a.download = 'file-download';
          document.body.appendChild(a);
          a.click();
          document.body.removeChild(a);
        }
 this._httpClient.post(AppUrl.DOWNLOADTOKEN, param).subscribe((response: any) => {
          const url = AppUrl.DOWNLOAD + '?token=' + response.data;
          const a = document.createElement('a');
          a.href = url;
//This download attribute will not change the route but download the file.
          a.download = 'file-download';
          document.body.appendChild(a);
          a.click();
          document.body.removeChild(a);
        }