1 回答
TA贡献1821条经验 获得超4个赞
您在接受文件时忽略了上传集。您需要使用扩展检查UploadSet.save()方法才能启动。
您还需要传入一系列扩展名,目前您传入一个字符串,添加一个逗号使其成为元组:
csv_file = UploadSet('files', ('csv',))
并在您看来使用:
@app.route('/datauploads', methods=['GET', 'POST'])
def datauploads():
if request.method == 'POST' and 'csv_data' in request.files:
filename = csv_file.save(request.files['csv_data'])
return render_template('details.html', filename=filename)
return render_template('index.html')
UploadNotAllowed但是,您可能想要捕获异常,否则会得到 500 错误:
from flask_uploads import UploadSet, configure_uploads, UploadNotAllowed
from flask import flash
@app.route('/datauploads', methods=['GET', 'POST'])
def datauploads():
if request.method == 'POST' and 'csv_data' in request.files:
try:
filename = csv_file.save(request.files['csv_data'])
return render_template('details.html', filename=filename)
except UploadNotAllowed:
flash('Only CSV files can be uploaded, please correct', 'error')
return render_template('index.html')
我使用了消息闪烁(Flask-Bootstrap 可以直接支持),但您index.html也可以更改为接受错误消息。
添加回答
举报