我通过 POST 接受该文件。本地保存时,可以使用file.read()读取内容,但是显示通过file.name不正确(16)的名称。当我尝试按此名称查找它时,出现错误。可能是什么问题?
我的代码:
@router.post(
path="/po/{id_po}/upload",
response_model=schema.ContentUploadedResponse,
)
async def upload_file(
id_po: int,
background_tasks: BackgroundTasks,
uploaded_file: UploadFile = File(...)):
"""pass"""
uploaded_file.file.rollover()
uploaded_file.file.flush()
#shutil.copy(uploaded_file.file.name, f'/home/fyzzy/Desktop/api/{uploaded_file.filename}')
background_tasks.add_task(s3_upload, uploaded_file=fp)
return schema.ContentUploadedResponse()
Run Code Online (Sandbox Code Playgroud) 如果在不使用验证器的情况下在字段中传递 None ,我可以在 pydantic 中设置默认值吗?
我有以下代码,但在我看来,这里的验证器对于contract_ndfl来说是多余的。有没有什么办法可以不用验证器呢?
我的代码:
class User(BaseModel):
user: int
s_name: str
contract_ndfl: Optional[int]
@validator('contract_ndfl')
def set_contract_ndfl(cls, v):
return v or 13
Run Code Online (Sandbox Code Playgroud)
如意代码:
class User(BaseModel):
user: int
s_name: str
contract_ndfl: Optional[int] = 13
Run Code Online (Sandbox Code Playgroud) 我想根据要求提供 xlsx。通过使用BytesIO,xlsxwriter我创建了一个文件。
使用下面的代码,我可以下载一个空(!).txt文件:
@router.get("/payments/xlsx", response_description='xlsx')
async def payments():
"""sss"""
output = BytesIO()
workbook = xlsxwriter.Workbook(output)
worksheet = workbook.add_worksheet()
worksheet.write(0, 0, 'ISBN')
worksheet.write(0, 1, 'Name')
worksheet.write(0, 2, 'Takedown date')
worksheet.write(0, 3, 'Last updated')
workbook.close()
output.seek(0)
return StreamingResponse(output)
Run Code Online (Sandbox Code Playgroud)
如果我添加,headers={'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'}我会在浏览器中收到此错误:
Unable to open file
You may be having a problem connecting with the server, or the file that you wanted to open was corrupted.
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?