Al final logré resolverlo. Pongo el código que me funcionó:
app.py
from flask import Flask, render_template, request, session, redirect, url_for
from werkzeug.utils import secure_filename
import sqlite3
import os
import datetime
from flask_wtf import FlaskForm
from wtforms import (StringField, BooleanField, DateTimeField,
RadioField,SelectField,TextField,
TextAreaField,SubmitField)
app = Flask(__name__)
app.config["SECRET_KEY"] = "hola"
con = sqlite3.connect("base.db") #conecto a la base
c = con.cursor() #defino el cursor
#c.execute("DROP TABLE Archivos")
c.execute("CREATE TABLE if not exists Archivos (Nombre TEXT, Fecha TEXT, Archivo BOLB)") #Crea la tabla
con.commit()
con.close()
@app.route('/', methods = ['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['file']
fecha = datetime.date.today()
con = sqlite3.connect("base.db") #conecto a la base
c = con.cursor() #defino el cursor
c.execute("INSERT INTO Archivos VALUES (?, ?, ?)", (f.filename, fecha, f.read()))
con.commit()
con.close()
return 'file uploaded successfully'
return render_template('upload.html')
@app.route("/download", methods=["GET", "POST"])
def download():
form = formarchivos()
if form.validate_on_submit():
session["nombrelec"] = form.nombrelec.data
#print(session["nombrelec"])
con = sqlite3.connect("base.db") #conecto a la base
c = con.cursor() #defino el cursor
c.execute("SELECT Archivo FROM Archivos WHERE Nombre= ?", ([form.nombrelec.data]))
arch = c.fetchone()
dire = "C:/Users/Usuario/Desktop/"+session["nombrelec"]
with open(dire, "wb") as output_file:
output_file.write(arch[0])
con.commit()
con.close()
return render_template("download.html", form=form)
class formarchivos(FlaskForm):
con = sqlite3.connect("base.db") #conecto a la base
c = con.cursor() #defino el cursor
c.execute("SELECT Nombre FROM Archivos")
lista = [line[0] for line in c]
con.commit()
con.close()
nombres = [(c,c) for c in lista]
nombrelec = SelectField('Elija nombre', choices = nombres)
buscar = SubmitField("Bajar")
if __name__ == '__main__':
app.run(debug = True)
y estos son los templates:
upload.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action = "/" method = "POST"
enctype = "multipart/form-data">
<input type = "file" name = "file" />
<input type = "submit"/>
</form>
</body>
</html>
dowload.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<title>Document</title>
</head>
<body>
<div class="jumbotron d-flex align-items-center"><div class="container text-center">
<h2>Bajar</h2><br>
<form class="form-group" method="POST">
{{ form.hidden_tag() }}
{{ form.nombrelec.label }}<br>{{ form.nombrelec(class="btn btn-primary dropdown-toggle") }}
<br>
<br>
{{ form.buscar(class="btn btn-success") }}
</form>
</div></div>
</body>
</html>