User presses a button contained within a form
on the page:
<form id="form">
<input type="button" id="export" value="Export"/>
</form>
Upon clicking the button, the following Ajax call is made:
ajaxCall('/export', {}, callback_export, 'get');
Where
function ajaxCall(url, params, callback, type) {
if (validate()) {
var request;
request = $.ajax({
url: url,
type: type,
data: params
});
}
request.done(function (response, textStatus, jqXHR){
callback(response);
});
}
The Flask app looks like this:
@app.route('/export')
def export():
xl_file= '/absolute/path/to/excel/file.xlsx'
return send_file(xl_file, as_attachment=True, mimetype='application/vnd.ms-excel')
The file contents of the file are being returned to the browser (see image below) but not the file itself as an attachment.
Question is, what does the callback need to look like to accept the response as a file attachment? Or otherwise, what modifications need to be made?
(Yes, I've searched and read many of the posts on SE. Most discussing using the form.submit()
method but do not provide details. I'm hoping to avoid using form.submit()
as there are other elements within the #form
that cannot be submitted.)