title: large files failing download in nodejs
body: <p>I have a service that builds a csv file, and returns it to the user. Currently using expressjs v4.14, nodejs v8.7.0. My problem is that I get a download failed due to 'network error' in chrome when I call the service for it to create a large csv file. With smaller files, the service works fine. I can also browse to the <code>/temp/</code> folder, and the entire expected file exists. In each case of 'things i tried', I was able to download smaller files but not the large ones.</p>
  
  <p><strong>Service:</strong></p>
  
  <pre><code>download.post('/csv', (req, res, next) =&gt; {
  
          res.status(200).header('Content-Type', 'text/csv');
          const newUUID = uuid.v1();
  
          let ws: WriteStream = fs.createWriteStream(`${__dirname}/../../temp/${newUUID}.csv`);
  
          ws.on('finish', () =&gt; {
              res.download(`${__dirname}/../../temp/${newUUID}.csv`);
          });
  
          //csv file built here 
          ws.write('huge stuff easily 50k rows and 10 mb file');
          ws.end(); 
  });
  </code></pre>
  
  <p><strong>Chrome Error:</strong></p>
  
  <p>Chrome's network tab and developer console do not give me any indication of what happened. This download popup is all I get. I cleared everything in cookies/cache just in case and it did not help.</p>
  
  <p><a href="https://i.stack.imgur.com/CK6Jw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CK6Jw.png" alt="enter image description here"></a></p>
  
  <p><strong>Things I tried:</strong></p>
  
  <ul>
  <li>writing chunks directly to response stream. </li>
  <li>using readable stream <a href="https://nodejs.org/api/stream.html#stream_readable_streams" rel="nofollow noreferrer">https://nodejs.org/api/stream.html#stream_readable_streams</a> , converting String into bytes and piping it. </li>
  <li>creating a file locally (writestream) and streaming it back (readstream pipe into res) </li>
  <li>res.download(file) after writestream creating local file ended </li>
  </ul>
  
  <p><strong>Update:</strong></p>
  
  <p>Ended up trying the service from postman and it worked, so I think it is an angularjs issue. </p>
  
  <p><strong>Angularjs</strong></p>
  
  <pre><code>    $http({
              cache: false,
              url: "/download/csv",
              headers: {
                  'accept': 'text/csv'
              },
              method: 'POST',
              data: {
                  rows: rows,
                  title: title ? title : ''
              }
          }).success(function (data, status, headers, config) {
          var anchor = angular.element('&lt;a/&gt;');
          anchor.attr({
              href: 'data:attachment/csv;charset=utf-8,' + encodeURI(data),
              target: '_blank',
              download: 'csv_info.csv'
          })[0].click();
      }).error(function (data, status, headers, config) {
  
      });
  </code></pre>