librelist archives

« back to archive

[flask] upload and download file

[flask] upload and download file

Pappu Kumar
2012-07-20 @ 13:48
Hi all,

I am trying to make a simple web application where one can upload a file, 
provide the file to a python script. The user can then download the file 
output from the script. Please let me know how can I parse the file in the
python script and get the output. So far I managed to do the following 
which uploads the file:

from flask import Flask
from flask import request
app = Flask(__name__)

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

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files['file']
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
  ['UPLOAD_FOLDER'], filename))
            return redirect(url_for('uploaded_file',
    return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload new File</h1>
<form action="" method=post enctype=multipart/form-data>
<p><input type=file name=file>
<input type=submit value=Upload>