[英]Python + Flask Image Upload "No file part"
I am new to python/flask and am trying to upload an image for a users profile picture during registration.我是 python/flask 的新手,正在尝试在注册期间为用户个人资料图片上传图像。 I've taken the backend flask code related to file uploads directly from their documentation found here .
我直接从他们在此处找到的文档中获取了与文件上传相关的后端烧瓶代码。 When I perform a registration, I receive the HTTP error:
当我执行注册时,我收到 HTTP 错误:
Bad Request The browser (or proxy) sent a request that this server could not understand.
Bad Request 浏览器(或代理)发送了该服务器无法理解的请求。
And in my python console:在我的 python 控制台中:
no file part
没有文件部分
I am using a bootstrap fork "Upload Image w Preview & Filename" found here .我正在使用此处找到的引导程序分支“上传带有预览和文件名的图像”。 Here is my registration form:
这是我的注册表:
<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>
And here is my backend/flask stuff.这是我的后端/烧瓶的东西。
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("/")
Add the attribute enctype='multipart/form-data'
to the form tag.将属性
enctype='multipart/form-data'
添加到表单标签。 As this attribute enforces the proper HTTP request handling of the form.因为此属性强制对表单进行正确的 HTTP 请求处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.