因爲昨天我試圖瞭解如何在另一個視圖中使用編碼的base64圖像從某個視圖。Python燒瓶阿賈克斯得到的圖像 - 最後編輯是問題
我需要將原始圖像的form.company_logo_image_path.data
替換爲調整大小的新圖像。新的調整大小的圖像通過AJAX發送到新視圖。
這裏我AJAX:
var dataurl = canvas.toDataURL("image/png");
$.ajax({
type: "POST",
url: "/profil/unternehmen-bearbeiten/resize-image",
data:{
a: dataurl
}
}).done(function() {
console.log('sent');
});
我創建該圖像進行解碼,並存儲在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
我通過將圖像保存到一個文件夾,用這種試驗我的本地機器和它的工作,所以身體變量正確地存儲調整大小的圖像。
現在我需要這個圖象發送到它會被上傳到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)
# ...
這裏的問題是,我得到一個Bad Request
的網站,如果我嘗試從第一個視圖訪問resize_image
函數的結果。我怎樣才能訪問新的圖像?
我的工作從昨天起這個問題,這是我曾經迄今最大的問題,這是我與我的進步古老的問題: Old question with my progress and tries
編輯
它無論我嘗試什麼,發送到"/profil/unternehmen-bearbeiten"
也會導致錯誤的請求錯誤。
請求數據a
任何地方產生了錯誤的請求:
try:
print request.values['a']
except Exception as e:
print "error", e
這裏唯一的例外是錯誤的請求
還請求本身導致了錯誤的請求在畫布上,在瀏覽器中犯規控制檯告訴我一些我可以使用/理解的有用的東西。它同在Eclipse的控制檯,它得到的路線400錯誤的請求,我嘗試發送至:
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請求,所以我需要正確的條件,它會工作(我猜)。
基本問題是: 我的Ajax請求,並在我發送給都POST
請求路由form.validate_on_submit():
,這就是爲什麼我經常收到錯誤的請求,有衝突。我正在嘗試創建一個自定義表單複選框。我試圖移動代碼和其他條件不同。我根本不明白。
我最近嘗試:
"""
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)
"""
你能張貼輸出到您的代碼片段在這裏? –
是否打印「,imagedata'打印? –
打印「The」,imagedata沒有提供任何內容,它在我猜測之前失敗,錯誤的請求 – Roman