fastApi 文件上传

from fastapi import FastAPI, File, UploadFile,Form

from starlette.responses import HTMLResponse
app = FastAPI()



@app.post("/create_file/")
async def create_file(file: bytes = File(...),names: str = Form(...)):
	try:
		f =open(names+".jpg","ab+")
		f.write(file)
		return {"filename":"ok"}
	except:
		return {"filename":"no"}
		
	


@app.get("/")
async def main():
    return HTMLResponse('''
    <body>

    <form action="/create_file/" enctype="multipart/form-data" method="post">
    	<input name="names" type="text" ><br><br>
        <input name="file" type="file" multiple><br><br>
      
        <input type="submit" value="上传文件">
    </form>
    
</body>
    
    
    
    ''')


if __name__ == '__main__':
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=81)

评论

刷新

友情链接