3

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?

GergelyPolonkai
  • 6,230
  • 6
  • 35
  • 69
KBKai
  • 353
  • 1
  • 4
  • 18
  • Have you tried that or you want us write code for you? – Remi Guan Sep 22 '15 at 06:45
  • I found some information,look like should use hooks plugin ...? But, I can't understand how to use it... – KBKai Sep 22 '15 at 06:51
  • Well, here is Stack Overflow. So I think you should Google your question first. And try to write some code. If your run your code and got an error, then you can ask here. – Remi Guan Sep 22 '15 at 06:56
  • I tried google my question first, but lots of result about flask framework... – KBKai Sep 22 '15 at 07:09
  • Hmm...you're right. But I think you still doesn't ask some question like this...Let me search about this again. – Remi Guan Sep 22 '15 at 07:21
  • So your problem is that you can't know the browser has finished downloaded the file or the file is downloading right? I've just see [the tutorial](http://bottlepy.org/docs/dev/tutorial.html) and I can't find any helpful things. – Remi Guan Sep 22 '15 at 07:28
  • OK... thanks for your response. I will apply for delete this question. – KBKai Sep 22 '15 at 07:35
  • I think you can keep this question, however this is a difficult question :) – Remi Guan Sep 22 '15 at 07:36
  • Yes, you are right. I don't know how to do function after browser request. – KBKai Sep 22 '15 at 07:37
  • However I can't solve this, so good luck ;) – Remi Guan Sep 22 '15 at 07:41

1 Answers1

0

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)  
Community
  • 1
  • 1
KBKai
  • 353
  • 1
  • 4
  • 18