本文介绍了Flask:如何处理应用程序/八位字节流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作多个文件上传表单.我使用 jQuery文件上传器.我的服务器端代码:

I want to make a multiple file-upload form.I use jQuery File Uploader. My server-side code:

@app.route("/new/photogallery",methods=["POST"])
def newPhotoGallery():
    print request.files

我尝试了两件事:

  1. 正常提交表单:

  1. Submit form normally:

当我正常提交表单时,它会打印:

When i submit my form normally,it prints:

ImmutableMultiDict([[''post_photo_gallery',FileStorage:u''('application/octet-stream'))])

使用AJAX提交表单:

Submit form using AJAX:

当我使用AJAX提交表单时,它会打印:

When i submit my form using AJAX,it prints:

ImmutableMultiDict([])

我的第一个问题是:为什么AJAX请求与普通请求之间会有区别.
我的第二个问题是:如何在Flask/Python 中处理此 application/octet-stream 请求我的第三个问题是:这是使用 application/octet-stream 的好方法吗?

My first question is: Why is there a difference between AJAX request and normal request.
My second question is: How can i handle this application/octet-streamrequest in Flask/PythonMy third question is: Is this a good way to use application/octet-stream ?

通过这种方式,我对 application/octet-stream 的了解不多.非常感谢.

By the way i do not know much about application/octet-stream.Thank you very much.

推荐答案

无论数据编码如何,您都应该能够通过 request.data 获取原始数据.如果是 application/octet-stream ,则只需将 request.data 写入二进制文件即可.

Regardless of the the data encoding, you should be able to get the raw data with request.data.In the case of application/octet-stream, you can just write request.data to a binary file.

各种数据类型的示例处理程序:

from flask import json

@app.route('/messages', methods = ['POST'])
def api_message():

    if request.headers['Content-Type'] == 'text/plain':
        return "Text Message: " + request.data

    elif request.headers['Content-Type'] == 'application/json':
        return "JSON Message: " + json.dumps(request.json)

    elif request.headers['Content-Type'] == 'application/octet-stream':
        with open('/tmp/binary', 'wb') as f:
            f.write(request.data)
            f.close()
        return "Binary message written!"

    else:
        return "415 Unsupported Media Type ;)"

已经在此处中记录了处理表单数据的典型方案.

The typical scenario of handling form data is already documented here.

这篇关于Flask:如何处理应用程序/八位字节流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 19:35