gpt4 book ai didi

Python + Flask 图片上传 "No file part"

转载 作者:行者123 更新时间:2023-11-30 22:18:05 25 4
gpt4 key购买 nike

我是 python/flask 新手,正在尝试在注册期间上传用户个人资料图片的图像。我直接从他们的文档中获取了与文件上传相关的后端 flask 代码here 。当我执行注册时,我收到 HTTP 错误:

Bad Request The browser (or proxy) sent a request that this server could not understand.

在我的 python 控制台中:

no file part

我正在使用 Bootstrap 分支“上传带有预览和文件名的图像”here 。这是我的注册表:

<form action="/register" method="POST">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" name="username" id="username" placeholder="Username">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" name="password" id="password" placeholder="Password">
</div>
<div class="form-group">
<label for="confirm-password">Confirm Password</label>
<input type="password" class="form-control" name="confirm-password" id="confirm-password" placeholder="Confirm Password">
</div>
<div class="form-group">
<label>Profile Picture</label>
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-default btn-file">
<button class="btn btn-success">Browse…</button>
<input type="file" id="imgInp" name="file">
</span>
</span>
<input type="text" class="form-control" readonly>
</div>
<img id='img-upload' style="margin-top: 10px;"/>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>

这是我的后端/ flask 内容。

from cs50 import SQL
from flask import Flask, flash, redirect, render_template, request, session, url_for
from flask_session import Session
from tempfile import mkdtemp
from werkzeug.exceptions import default_exceptions
from werkzeug.security import check_password_hash, generate_password_hash
from werkzeug.utils import secure_filename
import datetime
import os

UPLOAD_FOLDER = '/static/images'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.secret_key = "super secret key"
app.config["SESSION_TYPE"] = "filesystem"
db = SQL("sqlite:///data.db")
Session(app)

@app.route('/register', methods=['POST'])
def register():
username = request.form.get("username")
password = request.form.get("password")
row = db.execute("SELECT * FROM users WHERE username = :username", username=username)
if not row:
if 'file' not in request.files:
flash('No file part')
print("no file part")
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file')
print("no selected file")
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('uploaded_file',
filename=filename))
db.execute("INSERT INTO users (username, passwordHash, pictureUrl) VALUES (:username, :passwordHash, 'static/images/default.jpg')", username=username,passwordHash=generate_password_hash(password))
else:
return apology("Username already exists.")
return redirect("/")

最佳答案

将属性 enctype='multipart/form-data' 添加到表单标记。因为此属性强制执行表单的正确 HTTP 请求处理。

关于Python + Flask 图片上传 "No file part",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49459396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com