将文件保存在名称为用户名的文件夹中

时间:2018-11-08 17:11:16

标签: python file session flask

如何使用用户名将文件上传到文件夹。

当我在服务器上上传文件时-文件始终以随机名称保存在目录中。我该如何解决?

app.config['UPLOAD_FOLDER'] = os.path.dirname(os.path.abspath(__file__))

def login_required(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        if 'current_user' not in session:
            abort(401)
        if redis.get(session['current_user'])==None:
            abort(401)
        return f(*args, **kwargs)
    return wrapper



@app.route("/slyko/dl/upload", methods=['POST', 'GET'])
@login_required
def upload():
    token = request.form['token']
    try:
        user = jwt.decode(token.encode(), app.jwt_secret_key, algorithm='HS256')
    except jwt.ExpiredSignatureError:
        return abort(401)
    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'] + "/" + session.get("current_user"), filename))
    return redirect('http://127.0.0.1:5003/slyko/uploading', filename=filename)

0 个答案:

没有答案