html - Flask send_file as attachment not working -


i'm using flask's send_file method try , make browser download .txt file. problem browser not download anything.

here's python function:

@app.route('/download_zip', methods=['post']) def download_zip():     file_name = 'test.txt'     return flask.send_file(file_name, as_attachment=true, mimetype='text/plain') 

here's jquery function triggers post request:

function batchdownload() {     $.post('/download_zip', {         file_name: 'temp.zip'     }).done(function(data) {         alert(data);     }).fail(function() {         alert('error.  not download files :(');     }); } 

funny thing alert(data) in .done(...) callback displays file's content browser. browser receiving file content not downloading it.

any ideas?

thanks in advance!


edit

added form page:

<form id="download"></form>

and added .done(...) callback:

    $form = $('#download');     $form.submit(); 

i'm guessing need somehow link data (file) returned server response post request?

this not flask related question. it's how browsers , javascript works.

you're downloading file in ajax result passed ajax callback.

you want instead make browser download data in usual way.

to must use form , call .submit() method on it. create form in page hidden fields , submit in javascript function. should trick.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -