我想将pdf文件上传到文件夹,但文件未保存到文件夹。我已经阅读了flask文档中的上载文档,但是该文件未保存到我的文件夹中。我很困惑,没有错误
from flask import Flask, render_template, request, session, url_for, redirect, flash
from werkzeug.utils import secure_filename
UPLOAD_FOLDER = 'C:\\Users\\shyfa\\Documents\\Skripsi\\Program Fix\\uploads\\'
ALLOWED_EXTENSIONS = ['pdf']
app = Flask(__name__)
app.secret_key = "secret key"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
return '.' in filename and filename.rsplit('.',1)[1].lower in ALLOWED_EXTENSIONS
@app.route("/test", methods=['GET','POST'])
def upload_file():
if request.method == 'POST':
if 'file' not in request.files:
flash('No File Part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No Selected File')
return redirect(request.url)
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('upload_file', filename = filename))
return render_template('CobaInput.html')
if __name__ == "__main__":
app.run(debug=True)```
答案 0 :(得分:0)
第一个错误:您想要filename.rsplit('.',1)[1].lower()