Here is what I will do.
1. Create a prepare_file action
Firstly, I would create an action whose task would be to simply create a file and not display HTML, it will display a JSON object with the name of the created file.
2. Use AJAX to invoke the prepare_file action
, , " ..." AJAX . , AJAX, - .
3.
, JavaScript . send_file .
, , . , . , , . , , , , , .
, , , , .
, , .