Open Dev93junho opened 8 months ago
follow this code
@app.post("/image")
async def upload_image(file: UploadFile, db: Session = Depends(database.get_db)):
UPLOAD_DIR = "./image"
if not os.path.exists(UPLOAD_DIR):
os.makedirs(UPLOAD_DIR)
content = await file.read()
filename = file.filename
filesize = len(content)
random_uuid = str(uuid.uuid4())
print(random_uuid)
save_filename = random_uuid+"_"+filename
with open(os.path.join(UPLOAD_DIR, save_filename), "wb") as fp:
fp.write(content)
image = models.Image(uuid=random_uuid, save_dir=UPLOAD_DIR, img_category=None, img_name=filename, img_size=filesize)
# DATABASE
db.add(image)
db.commit()
#
response = RedirectResponse(url=f"/success?save_filename={save_filename}", status_code=303)
return response
The original code occured AttributeError: 'generator' object has no attribute 'add' from "/image"