This text will go over tips on how to add and obtain recordsdata utilizing a Flask database utilizing Python. Principally, we’ve a bit for importing recordsdata the place we will add recordsdata that can routinely save in our database. After we add a file and submit it, a message stating that your file has been uploaded and displaying the file title on the display seems. After we view our Flask SQLite database, we will see that our file has been routinely saved within the database. We will additionally obtain our recordsdata through the use of the /obtain /id quantity utilizing the hyperlink. Consequently, we will comprehend every part of this technique on this publish.
Importing and Downloading Information in Flask
For our add and return recordsdata with the database in a Flask, first, we create a templates folder for making select file and submit button in HTML file kind so let’s get began. To add and obtain recordsdata with the database in Flask, first we have to obtain SQLite DB browser to save lots of our knowledge in SQLite.
File construction

Templates File
Within the templates file, we’ll create just one HTML file which is able to function our all frontend half code.
index.html
Within the index.html file, we make the primary heading utilizing h1 for displaying precisely what we’re doing and after that, we write one kind by which we declare the tactic POST motion through the use of URL ‘/’ and after that, we initialize knowledge sort through the use of Enctype and we take the multipart/form-data on this so our all file will safely save in database and after that, we write code for easy enter button and in addition submit button for displaying all these functionalities clearly we’re utilizing some CSS in model tag for making our frontend half lovely
HTML
|
Output:

Html Output
app.py
After writing code for templates we create an app.py file exterior of the templates folder and create app,.py by which we’ll write our most important code of importing and returning recordsdata with a database in a Flask in Python language.
Step 1: Import all libraries
Within the app.py file we’ll write our most important a part of the code by means of these all operations will function simply first in app.py we have to import all vital libraries that are vital for doing add and returning recordsdata with database operations first we import io BytesIO this module will convert our all pdf recordsdata binary in beneath you’ll be able to see what sort of output will present within the database after we add any pdf within the database and we import render_template for rendering templates and after that, we’re importing send_file module which is able to assist us to ship the file to database and after that, we’re importing flask_sqlchemy for our SQL knowledge
Python
|
Step 2: Create a database
After importing all libraries we create an SQL database for importing and returning our file we initialize the Flask operate and after that, we make a database sqlite:///db.sqlite3 to save lots of our importing recordsdata and we create one DB as SQLAchemy saving database
Python3
|
Step 3: Create a desk for the database
after creating the database we have to make a desk database for which we create one ID which we make the first key after making the id we crate the filename and within the filename we initialize a varchar restrict of fifty and after that, we create one knowledge which is a blob which implies we will see ou file by click on on the blob and for making all these functionalities we write following strains of code in our app.py file
Python3
|
Step 4: Create an index operate
We create one index operate by which we set the primary path ‘/’ and after that, we cross the request for technique performance in technique performance we create one var add by which we initialize the filename and skim the file if we add a pdf so it should learn it in binary kind after penning this we create DB session in our database and in addition we commit our DB session after creating and after that we return file title with flashing therapeutic massage uploaded ‘ filename’ and we return our render template on index,.html file
Python3
|
After writing the code we have to create db.sqlite in our system to create a database we have to run the next command within the terminal:
python from app import app, db app.app_context().push() db.create_all() exit()

by working the above command within the terminal we will create a database by which our file will save after importing and submitting the database and we will see our database within the occasion folder routinely created for working tips on how to run these instructions watch a video which is connected beneath.
Step 5: Create a obtain operate for downloading the file.
After importing our file we have to make a obtain operate for obtain our importing file for obtain file we make one add variable by which we add an add question and we filter our recordsdata by id as we set each id distinctive so each file has a singular id and after that, we return our file through the use of send_file and in addition for obtain pdf file we convert it into binary to our knowledge file through the use of BytesIO module and after we will obtain our file through the use of the next hyperlink
http://127.0.0.1:5000/obtain/id_number
Python
|
Full Code
Python3
|
To run the above code we have to run the next command within the terminal:
flask run
Output:
After working these instructions within the terminal after we add recordsdata like pdf and pictures so following output will present within the database after we blob SQLite database.
Picture add
After we add photos so following picture sort interface will show on the display after we blob our database.

Database for picture
Pdf add
After we add any pdf file so database converts our pdf file into the binary kind so the next sort of interface will present after we add any pdf file within the database and the blob.

Database of pdf file in binary kind
Output Video: