title: Angular Express proper way to download file from server
body: <p>Basically the code below works. I click on button, the button sends the filePath data to the server. The server method <code>res.download</code> send the file as string. And then on the client a create anchor with href and download attributes. But when i try to download <code>.xls</code> file or <code>.zip</code> it downloads them but they are corrupted. I guess </p>
  
  <pre><code>'data:application/octet-stream,' + encodeURIComponent(response)
  </code></pre>
  
  <p>is causing a problem when the file is not .txt or .log</p>
  
  <p>Server:</p>
  
  <pre><code>app.post('/download', (req, res) =&gt; {
          let file = path.join(__dirname, 'public/files', req.body.filePath);
          res.download(file, req.body.filePath);
  });
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>function downloadFile(filePath) {
          return $http({
              url: '/download',
              method: "POST",
              data: { filePath: filePath }
          }).then((response) =&gt; {
              return response.data;
          }).catch(err =&gt; console.err);
  }
  
  function fileDownload(filePath, fileExtension) {
          let file = filePath + '.' + fileExtension;
          downloadFile(file).then((response) =&gt; {
              let a = document.createElement('a');
              a.setAttribute('style', 'display:none');
              a.setAttribute('href', 'data:application/octet-stream,' + encodeURIComponent(response));
              a.setAttribute('download', (filePath || 'Export') + '.' + fileExtension)
              document.body.appendChild(a);
              a.click();
              document.body.removeChild(a);
          });
      }
  </code></pre>