file_list = request.files.getlist('fileList')
if not file_list:
return error('', 'Please upload file')
file = file_list[0]
r = requests.get(File_url['get_guid'])
guid = r.text
file_name = file.filename
f = (file_name, file.read(), file.content_type)
request_data = {
'file': f,
'fileGuid': (None, guid),
'isVersionControl': (None, 'N'),
'isPrivate': (None, False)
}
ret = requests.post(File_url['upload_url'], files=request_data, headers={'accept': 'application/json'})
if ret.status_code != 200:
return error('', 'upload file error')
return success(guid, '')
做一个文件上传的需求,被困住了
1、flask 的文件是flaskStorage,而我们文件服务器所接受的是file
2、用multipart/form-data上传并不会被识别到。
问题1:
一篇文章的启发,发现POST files 中可以接收 file-tuple
file-tuple
can be a 2-tuple ('filename', fileobj)
, 3-tuple ('filename', fileobj, 'content_type')
or a 4-tuple ('filename', fileobj, 'content_type', custom_headers)
问题2:
Header里传’accept’: ‘application/json’ 还没搞懂为什么这样 参考的是 https://stackoverflow.com/questions/73818803/how-to-pass-multipart-form-data-into-python-post-request