0

I am trying to delete files after a user downloads them. In my app, the file name is passed from another script via a session variable.

The file structure is:

app
    routes.py
    --tmp<--location where files are downloaded and retrieved

This first snippet works: the file is successfully retrieved and downloaded, so I know the session variable is working.

def download_jrnlvl(): 
    output_file1 = session.get('session_variable', None)
    return send_file(output_file1, attachment_filename='jrnlvl_' + today + '.xlsx', as_attachment=True)

On a question I posted, I was referred to this answer and I have tried to apply the solution, using a generator to serve and then delete the file.

def download_outputfile(): 
output_file1 = session.get('output_file', None)
path = os.path.join(current_app.instance_path,output_file1)
def generate():
    with open(path) as f:
        yield from f
    os.remove(path)
r = app.response_class(generate(), mimetype='text/xlsx')
r.headers.set('Content-Disposition', 'attachment', filename='outputfile1.xlsx')
return r

The file that downloads is empty.

Using Flask Debug Toolbar I see the following under 'Request Vars' and I'm not sure how to apply this information to my predicament. Attempting absolute paths like below doesn't work:

('message', 'E:\python\flask\pminsts_v2.5.3\app\instance\tmp/tmp/output_020218.xlsx'), ('message', 'E:/tmp/tmp/output_020218.xlsx'), ('message', 'tmp/tmp/output_020218.xlsx'), ('message', '/tmp/tmp/output_020218.xlsx'), ('message', '/app/tmp/tmp/output_020218.xlsx'), ('message', 'E:/python/flask/pminsts_v2.5.3/app/tmp/tmp/output_020218.xlsx'), ('message', 'E:/python/flask/pminsts_v2.5.3/app/tmp\tmp/output_020218.xlsx'), ('message', 'E:/python/flask/pminsts_v2.5.3/app/tmp\tmp/output_020218.xlsx'), ('message', 'E:/python/flask/pminsts_v2.5.3/app/tmp/output_020218.xlsx'), ('message', 'E:/python/flask/pminsts_v2.5.3/app/tmp/output_020218.xlsx'), ('message', 'E:/python/flask/pminsts_v2.5.3/app/tmp/output_020218.xlsx'), ('message', 'E:/python/flask/pminsts_v2.5.3/app/tmp/output_020218.xlsx'), ('message', 'E:/python/flask/pminsts_v2.5.3/app/tmp/output_020218.xlsx'), ('message', 'E:/python/flask/pminsts_v2.5.3/app/instance/tmp\tmp/output_020218.xlsx'), ('message', 'E:\python\flask\pminsts_v2.5.3\app\instance\tmp/output_020218.xlsx'), ('message', 'E:\python\flask\pminsts_v2.5.3\app\instance\tmp/output_020218.xlsx'), ('message', 'E:/python/flask/pminsts_v2.5.3/app/tmp/output_combined_020218.xlsx')

In the code with the generator, I have tried an absolute path to the 'tmp' directory, and I am trying to get a handle on how to use instance_path. How do I get the correct path for the generator to work?

Grateful in advance.

EDIT:

I applied the technique in this answer, writing a class to remove the files after downloading. In my case, there are three files created. After one is selected the others get deleted but the selected file remains. I think this is because I am in Windows and there are restrictions on what Windows can do to open files. So, if that one file remains, I can live with that as it will get cleaned up the next time a user uses the site.

mattrweaver
  • 729
  • 4
  • 14
  • 36
  • 1
    What is `app?`(`r = app.response_class(generate(), mimetype='text/xlsx')`). Why not `r = current_app...`? – Danila Ganchar Feb 05 '18 at 10:31
  • Thanks for your response. That was a mistake on my part. I corrected it. I'm still getting empty excel files. – mattrweaver Feb 05 '18 at 13:46
  • Try to add `test endpoint` without `session` functionality and update the question. Make sure that file exists + body is not empty. try not to delete the file after `open`. – Danila Ganchar Feb 05 '18 at 13:59
  • The original file exists and is not empty. If I use "send_file" as in the first snippet, the file is downloaded and has the appropriate data. I'm new to flask and so I'm not sure about the "test endpoint" suggestion. Thanks – mattrweaver Feb 05 '18 at 15:00

0 Answers0