![](/img/trans.png)
[英]Python Flask Form - html Selectbox returning 'None' as the selected item
[英]flask jquery html form returning None for Image
web开发新手,尝试做一个简单的图片上传,目标是进入s3。 但目前当我 select 一张照片并提交请求时返回为无,无法弄清楚为什么。
html表格
<form name="update_profile_image_form" id="update_profile_image_form" class="text-center mt-2" method="POST" enctype="multipart/form-data" style="display: block;" action="/user/update/profile-image/">
<input type="file" class="form-control-file form-control-file-sm ml-3 mt-2" id="profile_image" name="profile_image" accept="image/*">
<input type="submit" value="Update" class="form-control-file form-control-file-sm ml-2 mt-2 text-center btn btn-sm btn-info" style="width: 90%;"></input>
</form>
脚本
$("form[name=update_profile_image_form").submit(function(e) {
var $form = $(this);
var $error = $form.find(".error");
var data = $form.serialize();
$.ajax({
url: "/user/update/profile_image",
type: "POST",
data: data,
dataType: "json",
success: function(resp) {
window.location.href = "/account/";
}
});
e.preventDefault();
});
路线
@app.route('/user/update/profile_image', methods=['POST'])
def update_profile_image():
return User().update_profile_image()
function
def update_profile_image(self):
img = request.files.get('profile_image')
print(img)
return redirect(url_for('account'))
也试过img = request.form.get('profile_image')
没有错误, print(img)
返回为无
目标是上传图片,传递给function,为其分配一个uuid并上传到s3,提前谢谢大家!
您不能序列化图像并将其作为 json 发送。
您应该使用表单数据 object 。 但是, print(img) 将不起作用,因为您无法以这种方式打印图像。
或者,您可以通过将图像转换为 base64来将图像作为字符串传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.