【问题标题】:create multipart form data with the same field names and different file types创建具有相同字段名称和不同文件类型的多部分表单数据
【发布时间】:2015-10-29 14:21:25
【问题描述】:

我在创建多部分表单数据时遇到问题。我需要有 3 个文件的数据(每个文件应该具有完全相同的字段名称)和 json 值。我正在使用 MultipartEncoder 来执行此操作,但要创建它使用字典的数据 - 这就是为什么我不能设置三个完全相同的字段值。我该怎么做?

这是我的一段代码,它可以工作,但是名为“mffc”的文件具有不同的字段名称,应该只称为“mfcc”

我的一段代码:

data = MultipartEncoder(
fields={
    'prototypeModel': ('prototypeModel', open(prototypeModel, 'rb'), 'application/octet-stream'),
    'mfcc_1': ('mfcc_1', open(mfcc_1, 'rb'), 'application/octet-stream'),
    'mfcc_2': ('mfcc_2', open(mfcc_2, 'rb'), 'application/octet-stream'),
    'mfcc_3': ('mfcc_3', open(mfcc_3, 'rb'), 'application/octet-stream'),
    'declaredParameters': json.dumps(declaredParameters)
}
)

print( '---------------------- start enroll ----------------------')
testEnrollResponse = requests.post(server+sessionID, data=data, headers={'Content-Type': data.content_type})
multipart_data = decoder.MultipartDecoder.from_response(testEnrollResponse)
userModel_out = multipart_data.parts[0].content

【问题讨论】:

    标签: python dictionary python-requests multipartform-data


    【解决方案1】:

    您可以使用元组列表,例如,

    data = MultipartEncoder(
        fields=[
            ('prototypeModel': (...)),
            ('mfcc', ('mfcc', ...)),
            ('mfcc', ('mfcc', ...)),
            ('mfcc', ('mfcc', ...)),
            (declaredParameters', ...),
        ]
     )
    

    【讨论】:

      猜你喜欢
      • 2016-01-25
      • 2017-03-03
      • 2012-10-05
      • 1970-01-01
      • 2016-09-28
      • 2016-01-05
      • 1970-01-01
      • 1970-01-01
      • 2012-07-27
      相关资源
      最近更新 更多