librelist archives

« back to archive

upload and download file

upload and download file

From:
Pappu Kumar
Date:
2012-07-22 @ 12:22
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)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('uploaded_file',
                        filename=filename))
    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>
</form
'''
app.run()

Thanks,
Pappu