Python Flask没有保存到文件夹

时间:2019-11-25 10:07:52

标签: python flask

我正在尝试创建一个Python / Flask应用,其中用户将图像保存到本地文件夹。它创建了文件夹,我获得了“已经上传”成功页面,但是图像没有保存在文件夹中

这是我的.py代码

import os
from flask import Flask, render_template, request

app = Flask(__name__)

APP_ROOT = os.path.dirname(os.path.abspath(__file__))

@app.route("/")
def index():
    return render_template("upload.html")

@app.route("/upload", methods=['GET','POST'])
def upload():
    target = os.path.join(APP_ROOT, 'images/')
    print(target)

    if not os.path.isdir(target):
        os.mkdir(target)

    for file in request.files.getlist("file"):
        print(file)
        filename = file.filename
        destination = "/".join([target, filename])
        print(destination)
        file.save(destination)

    return render_template("complete.html")

if __name__ == "__main__":
    app.run(port=4555, debug=True) 

这是我的.html代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>Upload here</h1>
<form id="upload-form" action="{{url_for('upload')}}" methods="POST" enctype="multipart/form-data">
    <input type="file" name="file" accept="image/*" multiple>
    <input type="submit" value="send">

</form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您在定义表单的html标记中有一个错字。它应该说:

<form id="upload-form" action="{{url_for('upload')}}" method="POST" enctype="multipart/form-data">

method="POST"相同,而不是methods="POST"