vendredi 5 janvier 2018

Flask application route fails

When i browse to my site url, aws.etc.etc\download\ , i get a 404 not found error. I thought i'd very explicitly set it to return test.txt to the user. Can anyone provide any ideas how to tackle this? It seems the AWS URL doesn't seem to be linking with the app route.

import os
from flask import Flask, request, redirect, url_for
from werkzeug.utils import secure_filename
from flask import send_from_directory

UPLOAD_FOLDER = '/tmp'
ALLOWED_EXTENSIONS = set(['inp', 'txt', 'png', 'jpg', 'jpeg', 'gif'])

application = Flask(__name__)

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

@application.route('/download/', methods=['GET'])
def download():
    return send_from_directory(directory='/tmp/', filename='test.txt', as_attachment=True)

@application.route('/', methods=['GET', 'POST'])
def upload_file():
    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 a 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))
            return redirect(url_for('upload_file',
                                    filename=filename))
    return '''
    <!doctype html>
    <title>Upload new File</title>
    <h1>Upload new File</h1>
    <form method=post enctype=multipart/form-data>
      <p><input type=file name=file>
         <input type=submit value=Upload>
    </form>
    '''

# run the app.
if __name__ == "__main__":
    # Setting debug to True enables debug output. This line should be
    # removed before deploying a production app.
    application.debug = True
    application.run()

Aucun commentaire:

Enregistrer un commentaire