【问题标题】:Downloading a compressed folder on client side not working in angular+node (MEAN)在客户端下载压缩文件夹在角度+节点(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;
            }}
        )

    }

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

【问题讨论】:

    标签: node.js angular download mean-stack tar


    【解决方案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);
            }
          );
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-07
      • 2014-08-30
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多