First step is to create a project root directory under which I will put all the required files for the project. Create the below app. This file should be created under the project root directory. Notice how I create flask instance. Next I will create main. In the above source code, the root path or endpoint will simply render the UI. This UI contains only one link for downloading a file from the server. Clicking on the link will ask user to save the file in a chosen location.
The file type could be anything. The latest stable version is Version 2. Ah yes, the good old problem of file uploads. The basic idea of file uploads is actually quite simple. It basically works like this:. The application accesses the file from the files dictionary on the request object. So first we need a couple of imports. Most should be straightforward, the werkzeug. Why do we limit the extensions that are allowed? Also make sure to disallow.
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. We can also configure the maximum upload size of the file. This is important because if the file is too large for the server, then the site may crash.
So, 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. For example, if we want to limit the maximum size of the file to 1 Mb, we need to add the following line of code in our program. After setting the maximum file size of the file in our python program, if we upload a file having a size of more than 1 Mb, then we will get the error as shown in the below image.
While building a file uploader using Flask, we can also configure the file extensions that we want to upload. To configure the allowed file extensions, we need to create a list of allowed extensions and check the uploaded file extension in the list. The below code shows a practical illustration of the method. In the above code, we create a function that will check the uploaded file extension and compare it with the allowed extensions.
Till now, we have seen how to upload a single file to our server. But sometimes, we also want to upload multiple files to the server. See the below code for illustration. After creating the Html form, we need to make some changes to our Flask application file. First, copy the following Python code into your Flask app file.
In the above code, we use python for loop to iterate over the list of files and then save each file one by one.
We can choose multiple files now and upload them at once to the server. We have seen how to upload files using Flask, but sometimes we also want to serve a file to the user. First, create an HTML template in the templates folder, named the file download. Now, we need to create our app.
In our app. Then, copy and paste the following code in the app. After copying the above code, run the code by typing the command python app. If everything works fine and the server starts without any problem, we have created our file downloader.
If you click the download button present in the URL, then the file will start downloading. For the above code to work properly, we need a file with the name sample. Let's create a new route and put this into practice, allowing our user to download a report by providing a path in the URL.
We're doing exactly the same as above, with the exception of adding the path prefix to the URL variable. The path should be relative from the reports directory saved in our app. Likewise any non-existent filenames would throw a error. The reason? I'm sure you wouldn't want users to be able to downlaod any file from your application at their own will. Read more about sending files in Flask over at the official documentation, linked here.
Drop the app. Using the sass, rcssmin and rjsmin Python packages to optimize web assets, including Bootstrap. Using Python decorators to add another layer of functionality to Flask routes.
0コメント