python - 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>
解决方案
您在定义表单的 html 标记中有错字。它应该说:
<form id="upload-form" action="{{url_for('upload')}}" method="POST" enctype="multipart/form-data">
如method="POST"
代替methods="POST"
推荐阅读
- javascript - 将 Salesforce 批量 api 数据解析成块进行处理
- java - Gradle 子项目未从本地 build.gradle 文件导入依赖项
- python - 如何搜索与特定模式匹配的 url?
- java - Springboot API 中带有 JPA 和 Spaghetti 代码的选项
- r - grpel 和 quanteda textstat_frequency 之间的不同频率计数
- c++ - sf::Event::Keypressed 在轮询事件循环内不起作用-SFML-2.5.1
- apache - request_URI 中特殊字符的 Apache 重定向规则
- python - 可以使用 Tweepy 发布图片,但不能发布视频
- git - git clone lfs 文件未完全下载
- html - 结尾的内容拆分和不以大写开头的标题