Bootstrap

python restful接口返回文件_通过Restful api接口上传文件

Restful api接口获取文件流的方法如下:

from requests_toolbelt import MultipartEncoder

with open('ch01.mp4', 'rb+') as f:

file_stream = f.read()

m = MultipartEncoder(fields={'project': '001', 'file_path' : 'ch01_08000000000000000.mp4', 'file_stream': ('filename', file_stream, 'text/plain')})

r = requests.post('http://0.0.0.0:8000/api/add-file/', data=m, headers={'Content-Type': m.content_type})

然后在接口中处理文件流:

with open(local_file_path, 'wb+') as f:

for i in file_stream.chunks():

f.write(i)

;