在客户端下载压缩文件夹在角度+节点(MEAN)中不起作用

Posted

技术标签:

【中文标题】在客户端下载压缩文件夹在角度+节点(MEAN)中不起作用【英文标题】:Downloading a compressed folder on client side not working in angular+node (MEAN) 【发布时间】:2019-09-18 13:43:56 【问题描述】:

我需要允许用户从服务器下载压缩文件夹。我设法进行了压缩,但是即使我尝试读取 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;
            
        )

    

文件未正确打开且大小不合适。 请帮忙

【问题讨论】:

【参考方案1】:

后控制器:

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

【讨论】:

以上是关于在客户端下载压缩文件夹在角度+节点(MEAN)中不起作用的主要内容,如果未能解决你的问题,请参考以下文章

节点重定向不适用于角度

CORS 标头在 MEAN 堆栈应用程序中不起作用

分页在角度 7 中不起作用。(我使用角度材料)

创建节点角度应用程序[关闭]

下载多个文件并以编程方式在客户端压缩它们

使用上下文和drawImage在角度客户端图像压缩上获取黑色图像