[英]flask file upload not working
我在上传文件时遇到问题。 我没有收到任何错误,但找不到文件。 我一直在寻找解决方案,但没有成功。 谁能帮助我? 我的代码:
UPLOAD_FOLDER = '/upload/'
ALLOWED_EXTENSIONS = set(['.xlsx', '.xls'])
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
print(filename)
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/file_upload', methods=['GET', 'POST'])
def file():
if request.method == 'POST':
print(os.getcwd())
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
print(filename)
filename = secure_filename(file.filename)
print(filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file', filename=filename))
return render_template("/file_upload.html", role="ok")
这是表格Flask Doc的示例
您的allowed_extensions
函数将拆分为"."
,即返回用"."
分隔的子字符串"."
,但ALLOWED_EXTENSIONS
仍包含"."
s。
将您的ALLOWED_EXTENSIONS
替换为
ALLOWED_EXTENSIONS = set(['xlsx', 'xls'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.