上传文件

概述

使用request的get_uploaded_files(self, key: str) -> List[MultipartFile]方法,可获取前端上传的文件列表。

MultipartFile对象的成员:

示例

example.py

from lessweb import Application, Request, MultipartFile

def upload(request: Request, id:int):
    f: MultipartFile = request.get_uploaded_files('f')[0]
    summary = f.value[:10] + b'...'
    return {'id':id, 'filename': f.filename, 'value': summary.decode(), 'size': len(f.value)}

app = Application()
app.add_post_mapping('/upload', dealer=upload)
app.run()

请求: curl -X POST --form id=6 --form f=@./example.py "http://localhost:8080/upload"
返回: {"id": 6, "filename": "example.py", "value": "from lessw...", "size": 360}