我怎样才能在烧瓶中上传文件

how can I make a upload file in flask

这是我的上传功能,但每次我转到 addclothes 路线并将图像添加到表单中时,我都会收到此错误:

我知道是我的路径问题,但我不知道如何将文件放在基目录中

@app.route('/addclothes', methods=["POST","GET"])
def addclothes():
    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 :
            filename = secure_filename(file.filename)
            file.save(os.path.join('/static/img/',filename))

            new_clothes = Clothes(clothesname=clothesname,image=image,price=price,isSuperUser=False)
            db.session.add(new_clother)
            db.session.commit()
            
            return redirect(url_for('addclothes'))



    return render_template('addclothes.html')

这是我的文件结构:

我想将它添加到基本目录

你应该保存它:

file.save(os.path.join('static/img/',filename))

注意删除静态前的/。有了这个礼物,它试图保存到 服务器 / 驱动器上的 static 子目录,而不是在应用程序的目录中。

附带说明一下,创建包含随机字符的文件名可能对您有利,而不是使用用户提供的文件名;当然,如果您要处理大量文件上传。请参阅我写的关于 .

的另一个主题