How to give download window option to user in a node.js express application
<p>I am building a node.js + express application. An excel file gets created on my server side and I need to serve it to the user in the download window. So the user can choose his desired directory and then download it. After the user does that, I want to then delete that file (which gets created in the app's local download folder).</p>
<p>Here is my code:</p>
<pre><code>router.post('/filter', function(req, res){
    try {
        var root = path.dirname(require.main.filename);
        var downloadsDir = root + '/downloads';
        var workbook = new Excel.Workbook();

        var worksheet = workbook.addWorksheet('My Sheet');
        worksheet.columns = [
            { header: 'Id', key: 'id', width: 10 },
            { header: 'Name', key: 'name', width: 32 },
            { header: 'D.O.B.', key: 'DOB', width: 10 }
        ];
        worksheet.addRow({id: 1, name: 'John Doe', dob: new Date(1970,1,1)});
        worksheet.addRow({id: 2, name: 'Jane Doe', dob: new Date(1965,1,7)});

        //ensure a download directory exist, if not then create it
        fs.ensureDir(downloadsDir, function (err) {
            console.log(err);
            workbook.xlsx.writeFile(downloadsDir + '/temp.xlsx').then(function() {
                console.log('file is written');
                var file = downloadsDir + '/temp.xlsx';

                // fs.emptyDir(downloadsDir + '/', function (err) {
                //  if (err) return console.error(err)
                //  console.log('success!')
                // });
                res.send(file);
            });
        });
    } catch(err) {
        console.log('OOOOOOO this is the error: ' + err);
    }
});
</code></pre>
<p>This code is called from an ajax function on my public javascript</p>
<pre><code>$('.filter-accounts-btn').click(function(){
        $.ajax({
            type: 'post',
            url: '/accounts/filter',
            success: function(data) {
                console.log('------- data: ' + data);
                window.open(data, '_blank');
            },
            error: function(err) {
                console.log('------------ Error: ' + err);
            }
        });
    });
</code></pre>
<p>However it does not seem to work. Obviously there is something wrong with it. When I run my app on Heroku, the new page that opens has the url :</p>
<blockquote>
<p><a href="https://myapp.com/app/downloads/temp.xlsx" rel="nofollow noreferrer">https://myapp.com/app/downloads/temp.xlsx</a></p>
</blockquote>
<p>It shows me error. I also tried using an npm module called <a href="https://www.npmjs.com/package/open" rel="nofollow noreferrer">open</a> where I would just open the excel for the user. But it caused the app to crash.</p>
<pre><code>open(file, function (err) {
    if ( err ) throw err;
});
</code></pre>
<p>When tested locally, it opens the excel.</p>
<p>Is there a better way to do this? Also, how would I know if the user has downloaded the file? Right now it is saved in the <strong>downloads</strong> folder, but I would like to empty that folder (using the commented code) as soon as the file is downloaded by the user.</p>
<p>************* Edit*********************</p>
<p>On suggestion, I have tried this new code</p>
<pre><code>workbook.xlsx.writeFile(tempfile('.xlsx')).then(function() {
    console.log('file is written');
    res.download(tempfile('.xlsx'), function(err){
        console.log('---------- error downloading file: ' + err);
    });
});
</code></pre>
<p>The res.downlaod throws an error</p>
<p><a href="https://i.stack.imgur.com/DvfY5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DvfY5.png" alt="enter image description here" /></a></p>
<blockquote>
<p>Error: ENOENT, stat
'/var/folders/nk/2w94bk3j5fs976d_dj5vwsvr0000gn/T/54f77d41-db1e-4dcf-8aea-7f88d682c436.xlsx'</p>
</blockquote>
<p>Is it not able to find the temp file?</p>