如何在运行时更改上传文件夹?我希望能够更改文件上传的位置,但我不知道如何。
我试过这样的事情,但我得到 KeyError :
@app.route('/upload', methods=['POST'])
def upload():
file = request.files['file']
path = 'uploads/text'
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
UPLOAD_FOLDER = path
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file',
filename=filename))
答案 0 :(得分:2)
你可以试试这个
file.save(os.path.join(/path/to/save/, filename))
在你的
UPLOAD_FOLDER = path
file.save(os.path.join(UPLOAD_FOLDER, filename))
答案 1 :(得分:0)
您可以尝试以下代码。它对我来说很完美。
base_path = os.path.abspath(os.path.dirname(__file__))
upload_path = os.path.join(base_path, app.config['UPLOAD_FOLDER'])
f.save(os.path.join(upload_path, secure_filename(f.filename)))