Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 当使用node和angular 5从linux服务器下载zip文件时,zip文件成功下载,但为空_Node.js_Angular - Fatal编程技术网

Node.js 当使用node和angular 5从linux服务器下载zip文件时,zip文件成功下载,但为空

Node.js 当使用node和angular 5从linux服务器下载zip文件时,zip文件成功下载,但为空,node.js,angular,Node.js,Angular,在我的services.service.ts文件中,我传递了一个zip名称并调用了路由 服务 downloadFile(name) { console.log(name); let newName = name+".zip"; console.log(newName); return this.http .get(`/services/downloadFile/${name}`, { responseType: ResponseContentType.Blob

在我的services.service.ts文件中,我传递了一个zip名称并调用了路由

服务

downloadFile(name) {
  console.log(name);
  let newName = name+".zip";
  console.log(newName);
  return this.http
    .get(`/services/downloadFile/${name}`, {
      responseType: ResponseContentType.Blob })
    .pipe(map(res => {
      return {
        filename: newName,
        data: res.blob()
      };
    }))
    .subscribe(res => {
      //  console.log('start download:',res);
        var url = window.URL.createObjectURL(res.data);
        var a = document.createElement('a');
        document.body.appendChild(a);
        a.setAttribute('style', 'display: none');
        a.href = url;
        a.download = res.filename;
        a.click();
        window.URL.revokeObjectURL(url);
        a.remove(); // remove the element
      }, error => {
        console.log('download error:', JSON.stringify(error));
      }, () => {
        console.log('Completed file download.')
      });
}
从service.js调用此路由后,zip文件将下载到windows,但没有数据 service.js

router.get('/downloadFile/:name', function (req, res) {
  let name = req.params.name;
  let newName = name+".zip";
  res.download(`./config/${newName}`, newName, function (err) {

    if (err) {
      console.log(err);
      console.log("ERROR APPEARED");
    } else {
      console.log('file uploaded :)');
    }
  });
});

此代码执行时没有错误(我将在控制台中获得“file Upload:)”),但下载的zip无法打开或提取,因为它不包含数据(适当的数据)

我曾经遇到过类似的问题-如果您使用基于令牌的身份验证,您可能忘记在标题中添加这些,请尝试:

.get(`/services/downloadFile/${name}`, {headers: new Headers({
      'Content-Type': 'application/json', // INFO: Format set to JSON
      'authorization': yourAuth.token.etc // INFO: Attach token
    }),responseType: ResponseContentType.Blob })
希望这能帮助你解决你的问题