Answers for "flask remove file after send_file"

0

flask remove file after send_file

@app.route('/files/<filename>/download')
def download_file(filename):
    file_path = derive_filepath_from_filename(filename)
    file_handle = open(file_path, 'r')
    @after_this_request
    def remove_file(response):
        try:
            os.remove(file_path)
            file_handle.close()
        except Exception as error:
            app.logger.error("Error removing or closing downloaded file handle", error)
        return response
    return send_file(file_handle)
Posted by: Guest on May-24-2021

Code answers related to "flask remove file after send_file"

Python Answers by Framework

Browse Popular Code Answers by Language