Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from fastapi import File, UploadFile
- from fastapi.responses import FileResponse
- #upload image
- @app.post("/uploadimage")
- # pip install python-multipart
- # buat terlebih dulu direktori /data_file untuk menyimpan file
- def upload(file: UploadFile = File(...)):
- try:
- print("mulai upload")
- print(file.filename)
- contents = file.file.read()
- with open("./data_file/"+file.filename, 'wb') as f:
- f.write(contents)
- except Exception:
- return {"message": "Error upload file"}
- finally:
- file.file.close()
- return {"message": f"Upload berhasil: {file.filename}"}
- # ambil image berdasarkan nama file
- @app.get("/getimage/{nama_file}")
- async def getImage(nama_file:str):
- return FileResponse("./data_file/"+nama_file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement