Python Send File To User Download Button UPDATED

Python Send File To User Download Button

Flask is one of the most popular frameworks for backend development. It is a microframework based on the python programming language. Instead, a micro-framework Flask is very powerful and is highly extensible. Since Python is popular for Auto Learning and Data Science, Flask is hugely used to serve Machine Learning and Data Science related apps. In this tutorial, we volition learn how to create a file uploader and file downloader using Flask.

File Uploading and downloading is an important task for a website. For example, assume that we were edifice a site that takes some images from user uploads and converts them into a single PDF. After converting the images into PDF, the website serves the PDF to the user. Nosotros need to learn to upload and download files using server-side frameworks like Flask in such a scenario.

Flask installation

We can install Flask easily with the help of the pip tool. PIP is a command-line tool, and it is the default package manager for managing python packages. To install the Flask framework, we need to run the following command in the terminal.

On running the above command in the terminal, we will accept Flask installed in our organisation. We can check our installation by opening the python shell and importing the Flask library. Run across the beneath paradigm for analogy.

checking the installation of flask by importing the library
checking the installation of Flask by importing the library

At present, let united states of america create our file uploader using Flask.

Creating the File Uploader

The first stride while creating our file uploader is to build the Html grade. To publish the file, we need to set the enctype holding of the form to "multipart/form-data". First, create a templates binder in the current directory. Later on that, create a file with the name upload.html in the templates folder and re-create the following HTML lawmaking into the upload.html file.

<!DOCTYPE html> <html lang="en">   <caput>     <meta charset="UTF-8" />     <meta http-equiv="10-UA-Compatible" content="IE=edge" />     <meta name="viewport" content="width=device-width, initial-calibration=1.0" />     <title>File uploader</title>   </head>   <torso>     <form       activeness="http://localhost:5000/upload"       method="Post"       enctype="multipart/form-data"     >       <input type="file" name="file" />       <input type="submit" />     </course>   </body> </html>

After creating the HTML template for the file uploader, now, in the primary directory, create a file with the proper noun app.py. The app.py file contains the code for our Flask application. We will also use the secure_filename() function of the werkzeug module. The secure_filename() module checks for vulnerability in the uploaded files and protects the server from dangerous files. Copy the following code into the app.py file.

# importing the required libraries from flask import Flask, render_template, asking from werkzeug.utils import secure_filename # initialising the flask app app = Flask(__name__)  # The path for uploading the file @app.road('/') def upload_file():    return render_template('upload.html')  @app.route('/upload', methods = ['Get', 'POST']) def uploadfile():    if request.method == 'POST': # cheque if the method is postal service       f = request.files['file'] # become the file from the files object       f.save(secure_filename(f.filename)) # this will secure the file       render 'file uploaded successfully' # Display thsi message afterwards uploading 		 if __name__ == '__main__':    app.run() # running the flask app

In the above lawmaking, we first imported the required modules to our code. After importing the required modules, we initialize the Flask app by calling the Flask() constructor. Side by side, nosotros apply python decorators to define the routes for our Flask app and serve the HTML template that we created. Next, we utilize python decorators to accept the post request for the file and save the file.

You lot can run the to a higher place lawmaking by only typing python app.py in the terminal. We will see something as shown in the below image while running the in a higher place code.

starting the flask server
starting the Flask server

At present, visit the URL http://127.0.0.ane:5000/ where you volition see a file uploader as shown in the below epitome.

a simple upload webpage in flask
a simple upload webpage in Flask

Upload a sample file past choosing a file and clicking the submit push. After submitting the file, we will get a bulletin showing that the file has been successfully uploaded. Nosotros tin can cheque the file in the server by visiting the root directory.

Configuring the upload folder

We created a file uploader that saves the uploaded file in the root directory. Nosotros can also configure the directory for saving the file by configuring the app.config['UPLOAD_FOLDER'] variable. At kickoff, the uploaded file is saved into a temporary location, so information technology moves to the final location. The following code shows how to configure the upload directory.

# importing the required libraries import os from flask import Flask, render_template, asking from werkzeug.utils import secure_filename  # initialising the flask app app = Flask(__name__)  # Creating the upload folder upload_folder = "uploads/" if non os.path.exists(upload_folder):    os.mkdir(upload_folder)  app.config['UPLOAD_FOLDER'] = upload_folder  # The path for uploading the file @app.route('/') def upload_file():    render render_template('upload.html')  @app.route('/upload', methods = ['GET', 'POST']) def uploadfile():    if asking.method == 'Mail service': # check if the method is post       f = request.files['file'] # get the file from the files object       # Saving the file in the required destination       f.save(bone.path.join(app.config['UPLOAD_FOLDER'] ,secure_filename(f.filename))) # this volition secure the file       return 'file uploaded successfully' # Brandish thsi message after uploading 		 if __name__ == '__main__':    app.run() # running the flask app

In the above code, we utilize the OS module of python to create a directory and saves every uploaded file to the created directory. If we restart the server and upload a file to the server, the file will be uploaded to the created directory instead of the root directory.

Configuring the maximum file size

We can as well configure the maximum upload size of the file. This is of import because if the file is too large for the server, then the site may crash. And then, we need to always limit the upload size of the file according to our server. To configure the maximum file size, we need to set the app.config['MAX_CONTENT_LENGTH'] variable to the maximum size of the file in bytes. For example, if nosotros want to limit the maximum size of the file to i Mb, we need to add the following line of code in our programme.

app.config['MAX_CONTENT_LENGTH'] = ane * 1024 * 1024

After setting the maximum file size of the file in our python program, if we upload a file having a size of more than than 1 Mb, then we volition get the mistake every bit shown in the beneath epitome.

setting the maximum upload size of a file in flask
setting the maximum upload size of a file in Flask

Configuring the allowed file extensions

While building a file uploader using Flask, nosotros can too configure the file extensions that we want to upload. To configure the allowed file extensions, nosotros demand to create a list of immune extensions and check the uploaded file extension in the list. The below code shows a applied illustration of the method.

# importing the required libraries import os from flask import Flask, render_template, asking from werkzeug.utils import secure_filename  # initialising the flask app app = Flask(__name__)  # Creating the upload folder upload_folder = "uploads/" if non os.path.exists(upload_folder):    bone.mkdir(upload_folder)  # Max size of the file app.config['MAX_CONTENT_LENGTH'] = 1 * 1024 * 1024  # Configuring the upload folder app.config['UPLOAD_FOLDER'] = upload_folder  # configuring the immune extensions allowed_extensions = ['jpg', 'png', 'pdf']  def check_file_extension(filename):     render filename.split('.')[-ane] in allowed_extensions  # The path for uploading the file @app.route('/') def upload_file():    return render_template('upload.html')  @app.road('/upload', methods = ['Become', 'Mail']) def uploadfile():    if asking.method == 'POST': # check if the method is post       f = request.files['file'] # get the file from the files object       # Saving the file in the required destination       if check_file_extension(f.filename):          f.save(os.path.join(app.config['UPLOAD_FOLDER'] ,secure_filename(f.filename))) # this will secure the file          return 'file uploaded successfully' # Display thsi message after uploading       else:          return 'The file extension is not immune' 		 if __name__ == '__main__':    app.run() # running the flask app

In the above code, we create a part that will bank check the uploaded file extension and compare it with the allowed extensions. If nosotros upload a file that does non have the extension of PDF, JPG, PNG, then the server volition show the message equally shown in the below image.

restricitng the upload of certain file extension in flask
restricting the upload of certain file extensions in Flask

Multiple file uploading using Flask

Till at present, we have seen how to upload a single file to our server. But sometimes, we also want to upload multiple files to the server. To upload multiple files to our server first, nosotros demand to add together multiple="true" every bit an aspect to the input element of the Html grade. Encounter the below lawmaking for illustration.

<!DOCTYPE html> <html lang="en">   <caput>     <meta charset="UTF-8" />     <meta http-equiv="X-UA-Compatible" content="IE=border" />     <meta proper noun="viewport" content="width=device-width, initial-scale=1.0" />     <title>File uploader</title>   </head>   <body>     <class       action="http://localhost:5000/upload"       method="POST"       enctype="multipart/form-data"     >       <input type="file" proper name="files", multiple="true" />       <input blazon="submit" />     </form>   </body> </html>

Afterwards creating the Html form, we need to make some changes to our Flask application file. Offset, copy the post-obit Python lawmaking into your Flask app file.

# importing the required libraries import os from flask import Flask, render_template, request from werkzeug.utils import secure_filename  # initialising the flask app app = Flask(__name__)  # Creating the upload folder upload_folder = "uploads/" if non os.path.exists(upload_folder):    os.mkdir(upload_folder)  # Configuring the upload folder app.config['UPLOAD_FOLDER'] = upload_folder  # configuring the allowed extensions allowed_extensions = ['jpg', 'png', 'pdf']  def check_file_extension(filename):     return filename.split('.')[-1] in allowed_extensions  # The path for uploading the file @app.route('/') def upload_file():    return render_template('upload.html')  @app.route('/upload', methods = ['Become', 'Postal service']) def uploadfile():    if request.method == 'Mail': # cheque if the method is postal service       files = request.files.getlist('files') # get the file from the files object       print(files)       for f in files:          print(f.filename)          # Saving the file in the required destination          if check_file_extension(f.filename):             f.relieve(os.path.bring together(app.config['UPLOAD_FOLDER'] ,secure_filename(f.filename))) # this volition secure the file        render 'file uploaded successfully' # Display thsi message afterwards uploading 		 if __name__ == '__main__':    app.run() # running the flask app

In the above code, we use python for loop to iterate over the list of files and then save each file one by one. Nosotros can cull multiple files at present and upload them at once to the server.

Serving the files

We take seen how to upload files using Flask, just sometimes we also desire to serve a file to the user. To serve a file from our server to the user, we need to utilise the send_file() office of the Flask. First, create an HTML template in the templates folder, named the file download.html, and add the following code into the Html file.

<!DOCTYPE html> <html lang="en">   <head>     <meta charset="UTF-8" />     <meta http-equiv="X-UA-Uniform" content="IE=border" />     <meta name="viewport" content="width=device-width, initial-calibration=1.0" />     <title>File Downloader</title>   </head>   <trunk>     <a href="/download"><push class='btn btn-default'>Download</button></a>   </body> </html>

The in a higher place HTML code will create a button with the label Download and add a link to the button that refers to the road /download. Now, we demand to create our app.py file. In our app.py, nosotros demand to employ the send_file() office and pass the file's path to it. And so, re-create and paste the following code in the app.py file.

# importing the required libraries import os from flask import Flask, render_template, request, send_file  # initialising the flask app app = Flask(__name__)  # displaying the HTML template at the dwelling url @app.route('/') def alphabetize():    render render_template('download.html')  # Sending the file to the user @app.road('/download') def download():    return send_file('sample.pdf', as_attachment=Truthful)  if __name__ == '__main__':    app.run() # running the flask app

Later copying the above code, run the lawmaking past typing the command python app.py. If everything works fine and the server starts without any trouble, we have created our file downloader. Now, visit the URL http://127.0.0.1:5000/, you lot will see something equally shown in the beneath image.

creating a file downloader using flask
creating a file downloader using Flask

If you click the download button present in the URL, then the file will outset downloading. For the above code to work properly, we need a file with the name sample.pdf in the app directory.

Determination

In this tutorial, nosotros learned to create a file uploader and file downloader using the Flask library. In addition, we have seen how to configure the max size and extension of the file upload. You may also want to run across our footstep-past-step guide on creating a Flask app in python.

DOWNLOAD HERE

Posted by: dunlapnotheeptist.blogspot.com

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel