I have built a flask app wherein the user uploads a file, it gets processed and thereafter, gets stored in a particular folder on the flask server itself. Now, I wanted to generate a download link for this file (a link I would be emailing to the user) instead of directly allowing the user to download the file. Any ideas how I should go about it? Thank You.
            Asked
            
        
        
            Active
            
        
            Viewed 1,013 times
        
    1 Answers
0
            
            
        You can use the following:
Note: Its recommended to use send_from_directory and keep the downloadable files in some specific directories permitted_directory so that the user can't download any file he wants from the whole server.
@app.route('/download_link/<path:filename>', methods=['GET', 'POST'])
def download_link():
   permitted_directory='/path/to/directoy/'
   return send_from_directory(directory=permitted_directory, filename=filename,as_attachment=True,cache_timeout=0)
then by using the serverURL/download_link/<filename>, the file will then be downloaded.
E.g.:
for serverURL=0.0.0.0:8080 and file=generated_file.csv
0.0.0.0:8080/download_link/generated_file.csv --> will be the download link for the file
You can use send_file as an alternative if you are not considering safe approaches and os.path.isfile() for handling unexisting files.
 
    
    
        mohamadmansourx
        
- 361
- 2
- 8
- 
                    So when I used send_from_directory, the server if giving an unhandled exception error while when I use send_file, it works fine....any specific reason for it? – Gap Jul 14 '21 at 18:14
 
    