I use python with bottle framework. When the user clicks a button, the server should generate file and let the user download it. When the user downloads it completely, the server should delete this file.
Is there any way to achieve this?
I use python with bottle framework. When the user clicks a button, the server should generate file and let the user download it. When the user downloads it completely, the server should delete this file.
Is there any way to achieve this?
 
    
     
    
    I found the way by this page (flask solution) and this page(hooks plugin) and this page(Forced Download).
this is my solution:
put hook "after_request" function into route function
part of my code:
download page templete (only one button):
<form id="export" calss="etable" method="POST" action="/download">
    <input type="submit" name="export" value="export">  
</form>
download page
@route('/download')
def download():
    output = template('download',template_lookup=['./views'])       
    return output
click button(generate file and redirect to download)
@route('/download',method = 'POST')
def downloadPost():
    zipname = zipFolder('./temp') #create files
    if zipname is not None:
        return redirect('/download/'+zipname)
    else:
        abort(500,'error.')
Forced Download function (put hook in this part.)
@route('/download/<filename:path>')
def download_file(filename):
    @hook('after_request')
    def delFiles():
        del('./temp/',filename) #delete file function       
    return static_file(filename, root='./temp/', download=filename)