我正在尝试创建一个简单的Web应用程序,可以上传文件并将文件提供给python脚本。烧瓶似乎适合这种用途。然后,用户可以从脚本下载文件输出。请让我知道如何解析python脚本中的文件并获取输出。到目前为止,我设法执行以下上传文件:
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()
答案 0 :(得分:1)
文件阅读在这里有详细记载:
http://docs.python.org/tutorial/inputoutput.html
获得文件后,您可以通过以下方式获取其内容:
f = open(os.path.join(app.config['UPLOAD_FOLDER'], filename),'r')
data = f.read()
答案 1 :(得分:0)
Flask不是处理文件上传任务的好方法。我推荐nginx file upload module