Return Files with Flask send_file Tutorial

In this Flask Web development tutorial, we're going to be discussing how to return files rather than templates.

Let's consider that we have a page with a download button for some file:
from flask import send_file

# ... other code ....

def file_downloads():
		return render_template('downloads.html')
	except Exception as e:
		return str(e)
{% extends "header.html" %}
{% block body %}
<body class="body">
   <div class="container" align="left">
		<a href="/return-files/" target="blank"><button class='btn btn-default'>Download!</button></a>
{% endblock %}

We can see that we're sending the person to /return-files/, which doesn't exist yet, let's make that.
def return_files_tut():
		return send_file('/var/www/PythonProgramming/PythonProgramming/static/images/python.jpg', attachment_filename='python.jpg')
	except Exception as e:
		return str(e)

In this case, if a person goes to /file-downloads/, and clicks the download button, they will get an image returned. How about a pdf?

def return_files_tut():
		return send_file('/var/www/PythonProgramming/PythonProgramming/static/ohhey.pdf', attachment_filename='ohhey.pdf')
	except Exception as e:
		return str(e)

Simple enough, but what if you want to protect the files in some way. On, for example, I let subscribers just download the videos, but you have to be a subscriber. Most downloads need to be in the static directory, which is totally public, so how would one go about protecting a file from the general public? That's what we'll be talking about in the next tutorial.

The next tutorial:

  • Introduction to Practical Flask
  • Basic Flask Website tutorial
  • Flask with Bootstrap and Jinja Templating
  • Starting our Website home page with Flask Tutorial
  • Improving the Home Page Flask Tutorial
  • Finishing the Home Page Flask Tutorial
  • Dynamic User Dashboard Flask Tutorial
  • Content Management Beginnings Flask Tutorial
  • Error Handling with Flask Tutorial
  • Flask Flash function Tutorial
  • Users with Flask intro Tutorial
  • Handling POST and GET Requests with Flask Tutorial
  • Creating MySQL database and table Flask Tutorial
  • Connecting to MySQL database with MySQLdb Flask Tutorial
  • User Registration Form Flask Tutorial
  • Flask Registration Code Tutorial
  • Finishing User Registration Flask Tutorial
  • Password Hashing with Flask Tutorial
  • Flask User Login System Tutorial
  • Decorators - Login_Required pages Flask Tutorial
  • Dynamic user-based content Flask Tutorial
  • More on Content Management Flask Tutorial
  • Flask CMS Concluded Flask Tutorial
  • The Crontab Flask Tutorial
  • Flask SEO Tutorial
  • Flask Includes Tutorial
  • Jinja Templating Tutorial
  • Flask URL Converters Tutorial
  • Flask-Mail Tutorial for email with Flask
  • Return Files with Flask send_file Tutorial
  • Protected Directories with Flask Tutorial
  • jQuery with Flask Tutorial
  • Pygal SVG graphs with Flask Tutorial
  • PayPal with Flask Web Development Tutorial
  • Securing your Flask website with SSL for HTTPS using Lets Encrypt