I have a button that triggers a GET request for a csv file. The server takes a few seconds to generate the file. While the file is being prepared, I want to disable the download button. What is the best way to achieve this with javascript / jquery?
I have this code that executes asynchronously - ideally this code should run synchronously - but perhaps there are different methods to achieve what I am trying to do?
// download csv:
$('#download-button').click(function(){
// 1) disable the button:
$(this).attr('disabled', true);
// 2) launch the file download on a new page:
document.target = '_blank';
document.location = "the-download-url";
// 3) enable the button (only after download is complete - need help here)
$(this).attr('disabled', false);
});
Edit
For the backend - I am using django - presently I am returning a table in the database as a csv file like so:
def view_function(response):
# logic to create csv file:
...
# pack csv file in response:
with open('/tmp/my-file.csv', 'r+') as file:
response = HttpResponse(file, content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename=item_mapping_service.csv'
return response
I am happy to change the backend code as needed if it helps.