在 angular+node (MEAN) 中无法在客户端下载压缩文件夹

Downloading a compressed folder on client side not working in angular+node (MEAN)

我需要允许用户从服务器下载压缩文件夹。我设法进行了压缩,但是即使我尝试读取 tar 文件并将其发送到客户端以供下载文件已损坏或仅保存文件夹中的一个文件(例如 1/7)。

我已尝试发送 json 个文件并且相同的代码有效,但对于大文件和 tar 个文件,它不会发送整个文件。 我试过只将 tar 文件从后端发送到客户端进行下载,但它也不起作用。

/* back controller. */
const getDataDownload = function(req,res) {
    if (req.params && req.params.dataId){
        DataModel
            .findById(req.params.dataId)
            .exec((err,data) => {
                if(data){
                    var file = '../data/filename.tar';
                    var stat = fs.statSync(file);
                    res.writeHead(200, {
                        'Content-Type' : 'application/tar',
                        'Content-Length' : stat.size
                    });
                    var tempTarFile = fs.createReadStream(file);
                    tempTarFile.pipe(res);
                }})}
    else {
            res
                .status(404)
                .json({"message":"No Id in request"});
        }
}
/* front controller. */
public dataDownload(dataId: string){
        const url: string = `${this.apiBaseUrl}/dataDownload/${dataId}/`;
        const req = new HttpRequest('GET', url, {
                headers: new HttpHeaders({'Authorization': token}), 
                responseType: 'text' as 'text', 
                reportProgress: true});
        this.http.request(req).subscribe((event: HttpEvent<any>)=>{
            switch (event.type) {
                case HttpEventType.Response:
                    const filename = 'filename.tar'
                    var blob = new Blob([event.body], { type: "application/tar" });
                    saveAs(blob, 'filename.tar')
                    break;
                case HttpEventType.DownloadProgress:
                    console.log(' (',Math.round(event.loaded/event.total),')');
                    break;
            }}
        )

    }

文件无法正常打开且大小不合适。 请帮忙

后台控制器:

const getDataDownload = function (req, res) {
  if (req.params && req.params.dataId) {
    DataModel
      .findById(req.params.dataId)
      .exec((err, data) => {
        if (data) {
          let file = '/home/pierbjx/Documents/sorfML/test.tar';
          try {
            let stat = fs.statSync(file);
            res.writeHead(200, {
              'Content-Type': 'application/tar',
              'Content-Length': stat.size
            });
            let tempTarFile = fs.createReadStream(file);
            tempTarFile.pipe(res);
          } catch (err) {
            console.log('Error: ' + err);
          }
        }
      })
  }
  else {
    res
      .status(404)
      .json({ "message": "No Id in request" });
  }
}

前控制器:

  public dataDownload(dataId: string): void {
    const url: string = `${this.apiBaseUrl}/dataDownload/${dataId}/`;
    const req = new HttpRequest('GET', url, {
      responseType: "blob",
      headers: new HttpHeaders().append("Content-Type", "application/json"),
      reportProgress: true
    });

    this.http.request(req)
      .subscribe(
        event => {
          if (event.type === HttpEventType.DownloadProgress) {
            console.log("Download progress event", event);
          }

          if (event.type === HttpEventType.Response) {
            console.log("response received...", event.body);
            const filename = 'filename.tar';
            saveAs(event.body, filename);
          }

        },
        err => {
          alert("Problem while downloading the file.");
          console.error(err);
        }
      );
  }