我正在开发一个基本的 Hug API,我的一个函数需要一个文件。
Hug 有办法上传文件吗?
这个例子就是你要找的:https ://github.com/timothycrosley/hug/blob/develop/examples/file_upload_example.py
@hug.post('/upload')
def upload_file(body):
"""accepts file uploads"""
# is a simple dictionary of {filename: b'content'}
print('body: ', body)
return {'filename': list(body.keys()).pop(), 'filesize': len(list(body.values()).pop())}
我认为可以。查看input_format.py,您应该能够提取编码一些 codex(url、utf-8 等)的文件。查看github自述文件,有这个例子:
@hug.default_input_format("application/json")
def my_input_formatter(data):
return ('Results', hug.input_format.json(data))
如果文件是 json 格式,那么您将从 json 对象中提取编码文件,将其转换为字节,然后将字节写入本地文件。