I try to save file on desktop and clear it after saving inside my application. I use Node.js and Express in my application. I got data from MongoDB and use fs.appendFile to save neccessary data to file. Then I use button to download saved information. Here is my code(search.ejs):
<div class="fileInfo" id="fileInfo">
<a href="files/2pac.doc" type="text/doc" charset="utf-8" download="2pac.doc">
<button type="button" class="btn btn-md saveToFile" id="saveToFile">
<!-- style="display:none;" id="saveToFile" -->
<i class="fa fa-download"></i>
Зберегти у файл
</button>
<!-- </a> -->
</div>
Then I use script in order to get access to function on serverside:
<script>
$('#saveToFile').click(function(){
//setTimeout($.get('/renewAfterDownload'), 5000);
$.get('/renewAfterDownload')
});
</script>
Here is my serverside code:
app.get('/renewAfterDownload', function (req, res){
fs.writeFile('public/files/2pac.doc', '', function(){console.log('done')});
})
I have problem: it clears file exactly after i click to download it, so it saves empty file. I have trying to use setTimeout, but it doesn't help. How can I fix this?