Ismail Hafeez
Ismail Hafeez

Reputation: 740

Removed send_file() command from my program but it still sends

I started learning Flask by myself today, and while I was toying around with it I made a small program to download a file, and it worked great.

@app.route('/download')
def download():
    path = "Path_to_my_file"
    return send_file(path, as_attachment=True)

Now I was changing my program a bit and I removed the 'send_file' function and added some other commands. However, when I tried running the program, the download still happened. I have no idea why though.

My code The download

My Code:

from flask import *
from flask_sqlalchemy import SQLAlchemy
from werkzeug.utils import secure_filename
import os


usera = ""

UPLOAD_FOLDER = 'I:/Python'
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config ['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///downloads.sqlite3'
db = SQLAlchemy(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user = db.Column(db.String)
    download = db.column(db.String)

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
    global usera, db
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit an empty part without filename
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            db.session.add(User(user = usera, download = filename))
            return render_template('lol.html')
    return '''
    <!doctype html>
    <title>Upload new File</title>
    <h1>Upload new File</h1>
    <form method=post enctype=multipart/form-data>
      <input type=file name=file>
      <input type=submit value=Upload>
    </form>
    '''

@app.route('/login',methods = ['POST', 'GET'])
def login():
    global db, user
    if request.method == 'POST':
        usera = request.form['user']
        return render_template('lol.html')

@app.route('/download')
def download():
    path = "I:/Python/"
    users = User.query.all()
    return users

if __name__ == '__main__':
   app.run(host='0.0.0.0')

Upvotes: 0

Views: 45

Answers (1)

Divakar Patil
Divakar Patil

Reputation: 323

I had faced same issue where flask was returning old files (in your case it is still sending files even if code is changed). This issue is due to the fact that file is cached in browser. Clear the browser cache and your api will start working.

Upvotes: 1

Related Questions