我有文件上传问题。我没有错误,但我无法找到文件。我一直在寻找解决方案,但没有成功。任何人都可以帮助我吗? 我的代码:
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
答案 0 :(得分:0)
您的allowed_extensions
功能正在"."
分割,即返回按"."
分割的子字符串,但ALLOWED_EXTENSIONS
仍包含"."
。
将您的ALLOWED_EXTENSIONS
替换为
ALLOWED_EXTENSIONS = set(['xlsx', 'xls'])