- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
从昨天开始,我一直在尝试了解如何在另一个 View 中使用来自某个 View 的编码 base64 图像。
我需要用调整大小的新图像替换原始图像的 form.company_logo_image_path.data
。新调整大小的图像通过 AJAX 发送到新 View 。
这是我的 AJAX:
var dataurl = canvas.toDataURL("image/png");
$.ajax({
type: "POST",
url: "/profil/unternehmen-bearbeiten/resize-image",
data:{
a: dataurl
}
}).done(function() {
console.log('sent');
});
我创建了一个新 View ,其中图像被解码并存储在 body
变量中:
@app.route('/profil/unternehmen-bearbeiten/resize-image', methods=["POST"])
def resize_image():
data_url = request.values['a']
content = data_url.split(';')[1]
image_encoded = content.split(',')[1]
body = base64.decodestring(image_encoded.encode('utf-8'))
return body
我通过将图像保存到本地计算机上的文件夹来对此进行测试,它起作用了,因此 body 变量正确存储了调整大小的图像。
现在我需要将这张图片发送到另一个 View ,在那里它将上传到 AWS3,我将使用这张图片而不是 form.company_logo_image_path.data
:
@app.route('/profil/unternehmen-bearbeiten', methods=["GET", "POST"])
@login_required
@check_confirmed
def edit_company():
the_company = Company.query.filter_by(users_id=current_user.id).first()
form = EditCompanyForm(obj=the_company)
used_file = the_company.company_logo_image_path
if form.validate_on_submit():
form.populate_obj(the_company)
imagedata = resize_image()
print "The", imagedata
if form.company_logo_image_path.data:
upload_image_to_aws('baubedarf', "userimages/", form.company_logo_image_path, the_company,'company_logo_image_path', the_company.company_name)
# ...
这里的问题是,如果我尝试从第一个 View 访问 resize_image
函数的结果,我会得到一个 Bad Request
站点。如何访问新图像?
我从昨天开始就在研究这个问题,这是我迄今为止遇到的最大问题,这是我的老问题和我的进展: Old question with my progress and tries
编辑
不管我尝试什么,发送到 "/profil/unternehmen-bearbeiten"
也会导致错误的请求错误。
在任何地方请求数据 a
都会导致错误请求:
try:
print request.values['a']
except Exception as e:
print "error", e
这里的异常是错误的请求
请求 Canvas 本身也会导致错误请求,浏览器中的控制台不会告诉我一些我可以使用/理解的有用信息。它与 Eclipse 中的控制台相同,它在我尝试发送到的路由中收到 400 Bad Request:
try:
print request.form['uploading_canvas']
except Exception as e:
print "error 1", e
编辑
我终于取得了一些重大进展!我试图在 if form.validate_on_submit():
中请求数据。我现在将代码放在 if form.validate_on_submit():
之外,我现在可以请求数据,我仍然遇到问题,但从这里我可以继续工作!
if request.method == 'POST':
print "post"
file_data = request.values['a']
imagedata = resize_image(file_data)
print "The", type(imagedata)
if form.validate_on_submit():
# ...
我在这里再次收到错误请求,但我现在明白为什么了。 form.validate_on_submit():
本身也是一个 POST 请求,所以我需要正确的 if 条件,它会起作用(我猜)。
基本上问题是:我的 ajax 请求和我发送到的路由中的 form.validate_on_submit():
都是 POST
请求,这就是为什么我经常收到 Bad Request 的原因,那里是冲突。我正在尝试创建一个自定义表单复选框。我正在尝试移动代码和其他不同的 if 条件。我就是不明白。
我最近的尝试:
"""
if form.company_check_it.data == True:
print "post 1"
file_data = request.values['a']
imagedata = resize_image(file_data)
print "The", type(imagedata)
else:
print "post 3"
"""
"""
if request.method == 'POST':
print "post"
file_data = request.values['a']
imagedata = resize_image(file_data)
print "The", type(imagedata)
"""
if form.validate_on_submit():
print "post 2"
"""
if form.company_check_it.data == True:
print "post 1"
file_data = request.values['a']
imagedata = resize_image(file_data)
print "The", type(imagedata)
else:
print "post 3"
"""
if request.method == 'POST':
print "post"
file_data = request.values['a']
imagedata = resize_image(file_data)
print "The", type(imagedata)
form.populate_obj(the_company)
"""
data_url = request.values['a']
print data_url
content = data_url.split(';')[1]
image_encoded = content.split(',')[1]
body = base64.decodestring(image_encoded.encode('utf-8'))
print type(body)
"""
最佳答案
我认为您需要一种不同的方法,因为您似乎混淆了 HTML 表单/Flask View /Javascript 之间传递的所有数据。
基本上,您的 Javascript 应该使用 Canvas 中已调整大小的图像的 dataURL 填充表单中的隐藏字段。然后这将被提交到您的 Flask View ,您可以在其中将数据上传到 S3。
下面是一个简单的示例应用程序,说明了我的意思。
app.py
from flask import Flask, url_for, redirect, render_template
from flask_wtf import Form
from wtforms import HiddenField
import base64
class EditCompanyForm(Form):
resized_image = HiddenField()
app = Flask(__name__)
app.config['SECRET_KEY'] = '1234'
@app.route("/", methods=['GET', 'POST'])
def index():
form = EditCompanyForm()
if form.validate_on_submit():
if form.resized_image.data:
data = resize(form.resized_image.data)
print("Uploading to AWS")
# upload_image_to_aws(data, other_variables_as_needed)
return redirect(url_for('index'))
return render_template('index.html', form=form)
def resize(data_url):
content = data_url.split(';')[1]
image_encoded = content.split(',')[1]
body = base64.decodestring(image_encoded.encode('utf-8'))
return body
if __name__ == "__main__":
app.run(debug=True)
templates/index.html
<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var imageObj = new Image();
imageObj.onload = function() {
context.drawImage(imageObj, 69, 50);
};
imageObj.src = '{{ url_for('static', filename='darth-vader.jpg') }}';
$("#submit").click(function() {
var dataurl = canvas.toDataURL("image/png");
$("#resized_image").val(dataurl);
});
});
</script>
</head>
<body>
<canvas id="myCanvas" width="578" height="400"></canvas>
<form method="post">
{{ form.hidden_tag() }}
<input type="submit" value="Save" id="submit" name="submit" />
</form>
</body>
</html>
关于Python flask ajax 获取图像 - 最后编辑是问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42806861/
我正在遵循Flask-potion教程并尝试保护我的api。我一直试图找出如何在User端点上设置权限,以使一个User无法查看另一个User的记录。这是我的代码: class UserResourc
项目设置 Python 3.5.3 flask 0.12.2 目录 . ├── Core │ ├── BackgroundProcessManager.py │ ├── FirebaseDat
我正在构建 flask 应用程序并决定这次尝试应用程序工厂方法,但是遇到了 flask 迁移的麻烦并且无法找出简单的解决方案。 请注意,我想将配置位置作为选项传递给脚本 管理.py: manager
我尝试通过以下示例代码来熟悉Flask-APScheduler插件:https://github.com/viniciuschiele/flask-apscheduler/blob/master/ex
我一周前刚刚了解了 Flask 和 JWT,所以我真的需要一些帮助。有人可以解释一下flask-jwt和flask-jwt-extended之间的区别吗? 最佳答案 Flask-JWT 没有那么多功能
我是 Flask 的新手,我只是想用它创建一个简单的市场。我的网站上有三种类型的用户 [卖家、买家、管理员]。我认为每种类型的用户都应该有自己的注册和登录,因为他们的角色应该提供不同类型的信息。另一方
基本上,我想做一些非常简单的事情:我想创建一个用于删除数据库中条目的表单。 模板正在创建一个包含所有条目的html表,没有任何麻烦。我现在的问题是:如何将其转换为每行都有链接的表单。 当然,我可以通过
我对Flask来说非常陌生,其中一个基本要求是我需要SEO友好的URL。 我说一条路线 @app.route('/sales/') @app.route(/sales/) def get_sales(
一直在为用户管理系统开发Flask-user,但是我看不到自定义登录页面和注册页面的方法。我使用pip安装了flask-user pip install flask-user 任何帮助,将不胜感激 最
我有一个奇怪的情况,不知道如何找到问题。 我已经创建了一个这样的蓝图 reportjobsmod = Blueprint('jobreports', __name__, url_prefix='/jo
我认为在 Flask 中拥有实例变量的正确方法是添加用户和 session ,但我正在尝试测试一个概念,我还不想经历所有这些。我试图让一个网络应用程序将图像加载到一个变量中,然后可以对其执行不同的图像
select = SelectField("City") form.select.choices = [(places['name'], places['name']) for places in p
这个问题在这里已经有了答案: Provide extra information to Flask's app.logger (2 个回答) 3年前关闭。 我正在运行一个 flask 应用程序。我想写
我当时正在编程一个非常不错的api,使用json从游戏服务器到我的网站空间获取一些json数据, 但是每次我使用angular发送请求时,我都会得到以下信息:127.0.0.1--[20 / Mar
我已经开发了一个上传表单,以将特定的.xlsx文件作为上传。要求是要处理非xlsx上载的任何异常(例如zip,exe文件)。我正在使用pyexcel库读取上传内容。我尝试创建以下代码来处理此异常: e
我使用此功能在网站上捕获404错误: @app.errorhandler(404) def page_not_found(e): logger.warning('User raised an
我正在尝试同时使用 Flask Restful 和 Flask SocketIO。我已经制作了一个 Flask Restful 应用程序,但现在我想使用 Flask SocketIO 在客户端和我的服
我有一些 Flask 应用程序,我通过设置 FLASK_APP 来运行它们。环境变量和调用 flask run .自 FLASK_DEBUG也设置了,每当我更改一些代码时,应用程序都可以方便地重新加载
我目前正在使用 Flask 构建一个应用程序。我正在努力访问 Flask 实例“app”以及 Flask-mail 实例“mail”。 下面是我的项目的样子: └── my-project ├
Remember-me是 parameter flask_login.login_user(user, remember=False, duration=None, force=False, fres
我是一名优秀的程序员,十分优秀!