如何读取烧瓶中的多部分/形式数据 [英] how to read multipart/form-data in flask

查看:40
本文介绍了如何读取烧瓶中的多部分/形式数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法读取通过XMLHttpRequest发布的烧瓶中的数据.我正在使用这个jquery插件来裁剪图像并上传到服务器

I'm having trouble reading data in flask that is posted via XMLHttpRequest. I'm using this jquery plugin to crop the image and upload to server

https://codecanyon.net/item/slim-image-cropper-active-uploadingandandratio-cropping-plugin/16364167

数据-在使用XMLHttpRequest将图像发送到服务器之前,有关图像的信息已收集并包含在json中

data - information about the image is collected and coverted in json before sending it to server using XMLHttpRequest

var formData = new FormData();
var name = "ex"; // Just an example
formData.append(name, JSON.stringify(data));
var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.send(formData);

在我的烧瓶代码中,我打印请求的标题以查看内容类型

In my flask code, I print the header of request to see the content type

print(request.headers)

我看到了

Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8,sv;q=0.6,fr;q=0.4
Host: localhost:5000
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary5e7lMMIQavXzSZg9
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36
Connection: keep-alive
Content-Length: 62714
Origin: http://localhost:5000
Pragma: no-cache
Cache-Control: no-cache
Cookie: session=eyJfZnJlc2giOmZhbHNlLCJjc3JmX3Rva2VuIjoiYTJhNTNlYzRkYjZlYzgzNzM2NDQ1ZjM5ZDAxNmY0MTlmY2RiZmRiOCIsInVzZXJuYW1lIjoidXNlcjEifQ.Cv2ADg.d8chDgzAA7fS2P9KcwzRINvLGOU
Referer: http://localhost:5000/loadProfile
Accept: */*

我无法使用

request.get_json()

request.get_json()

如果我尝试

print(request.get_json())
data = request.get_json()
print(data['name'])

TypeError:"NoneType"对象不可下标

TypeError: 'NoneType' object is not subscriptable

在HTTP发布请求中,我可以看到

In HTTP post request I can see this

------ WebKitFormBoundary0okhJV9YZK0uArTm内容处置:表单数据;name ="slim []"{服务器":[{状态":成功"}}],元":{},输入":{名称":"sample-img.jpg",类型":图像/jpeg",大小":41319,宽度":400,高度":300},输出":{宽度":400,高度":300,图像":"data:image/jpeg; base64,/9j/.......

------WebKitFormBoundary0okhJV9YZK0uArTm Content-Disposition: form-data; name="slim[]" {"server":[{"status":"SUCCESS"}],"meta":{},"input":{"name":"sample-img.jpg","type":"image/jpeg","size":41319,"width":400,"height":300},"output":{"width":400,"height":300,"image":"data:image/jpeg;base64,/9j/.......

根据作者

Slim使用POST请求以JSON格式发送数据(包含base64编码的图像).

格式:

{
   "server":null,
   "meta":{

   },
   "input":{
      "name":"stars.jpg",
      "type":"image/jpeg",
      "size":null,
      "width":800,
      "height":1200
   },
   "output":{
      "width":150,
      "height":100,
      "image":"data:image/jpeg;base64..."
   },
   "actions":{
      "rotation":0,
      "crop":{
         "x":0,
         "y":333.33333333333337,
         "width":150,
         "height":100.10460251046025,
         "type":"manual"
      },
      "size":null
   }
}

如果我这样做

print(request.form)

我可以看到

ImmutableMultiDict(((('slim []','{"server":[{"status":"SUCCESS"}]],"meta":{},"input":{"name":"sample-img.jpg,"类型:"图像/jpeg,"尺寸:41319,"宽度:400,"高度:300},"输出:{"宽度:400,"高度:300,"image":"data:image/jpeg; base64,/9j/4AAQ ....

ImmutableMultiDict([('slim[]', '{"server":[{"status":"SUCCESS"}],"meta":{},"input":{"name":"sample-img.jpg","type":"image/jpeg","size":41319,"width":400,"height":300},"output":{"width":400,"height":300,"image":"data:image/jpeg;base64,/9j/4AAQ....

但是我不确定如何读取此帖子数据.例如获取名称的值

But I'm not sure how to read this post data. For e.g get value of name

推荐答案

您可以使用request.form.to_dict()将结果转换成字典形式.

You can use request.form.to_dict() to get result into dictionary form.

或者您可以使用类似这样的内容:

Or you can use something like this:

导入ImmutableMultiDict数据= dict(request.form)打印数据

这篇关于如何读取烧瓶中的多部分/形式数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆