pappu
pappu

Reputation: 137

web interface to python program: upload and download files

I am trying to make a simple web application where one can upload a file and provide the file to a python script. Flask seems appropriate for such purpose. 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()

Upvotes: 0

Views: 4699

Answers (2)

JiaJie999
JiaJie999

Reputation: 118

Flask is not the good way to handle file upload task. I recommend nginx file upload module

Upvotes: 0

Yasser
Yasser

Reputation: 1808

File reading is well documented here:

http://docs.python.org/tutorial/inputoutput.html

Once you have a file, here is how you can get its contents:

f = open(os.path.join(app.config['UPLOAD_FOLDER'], filename),'r')
data = f.read()

Upvotes: 1

Related Questions