【问题标题】:How to save an image into a folder inside the static folder using HTML form input type file and flask如何使用 HTML 表单输入类型文件和烧瓶将图像保存到静态文件夹内的文件夹中
【发布时间】:2020-10-05 21:40:22
【问题描述】:

在我的烧瓶应用程序中,我希望用户能够将图像上传到静态文件夹内的文件夹 - 称为壁纸。目前我收到 flask.debughelpers.DebugFilesKeyError,但是我没有看到我正在使用的键有任何错误


我尝试过的


flaskapp.py

@app.route('/changeWallpaper' , methods = ['POST', 'GET'])
def change_home_wallpaper():

    UPLOADS_PATH = join(dirname(realpath(__file__)), 'static\\wallpaper')

    if request.method == "POST":
        wallpaper = request.files['wallpaper']
        if wallpaper.filename != '':
                image = request.files['wallpaper']
                image.save(os.path.join(UPLOADS_PATH, secure_filename(image.filename)))

        cur = db2.cursor()
        sql = f"UPDATE wallpaper set pic = {wallpaper.filename} where sno = 1"
        cur.execute(sql)
        db2.commit()
        cur.close()
        return redirect(url_for('home'))
    else:
        return redirect(url_for('home'))

loggedin.html

<div class="jumbotron">
    <form action="{{url_for('change_home_wallpaper')}}" method="post">
        <div class="container">
            <input type="file" name="wallpaper"/>
            <input type="submit" class="btn btn-primary"/>
        </div>
    </form>

</div>

【问题讨论】:

  • 在这里查看stackoverflow.com/a/54559035/8843270,这可能会给你一些线索
  • @simkus 谢谢我必须包含enctype="multipart/form-data",并将{wallpaper.filename} 放在引号中。您介意将其添加为答案吗

标签: python html forms flask


【解决方案1】:

更新您的HTML

<form action="/path" method="post" enctype="multipart/form-data">
</form>

并将{wallpaper.filename} 放在引号中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多