receive_nowait raise WouldBlock error while uploading multiple files in FastAPI

311 Views Asked by At

I am trying to send multiple files from UI to fastAPI in formData. But the API is throwing error instantly when the API's is triggered.

    async def createNewDirectory(files: Optional[List[UploadFile]] = 
       File(None), content: list = [], db: Session = Depends(get_db)):
    try:
        print(content)
        for file in files:
            try:
                contents = file.file.read()
                with open(file.filename, 'wb') as f:
                    f.write(contents)
            except Exception:
                return {"message": "There was an error uploading the 
    file(s)"}
            finally:
                file.file.close()

Below is the error i am getting

INFO:     Started server process [70987]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to 
quit)
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/site-packages/anyio/streams/memory.py", line 81, in receive
   return self.receive_nowait()
  File "/site-packages/anyio/streams/memory.py", line 76, in 
receive_nowait
  raise WouldBlock
I am using lastest FastAPI, Starlette version

fastapi==0.100.0
uvicorn==0.22.0
starlette==0.27.0
1

There are 1 best solutions below

0
On

You should check if you're sending the files as 'multipart/form-data'.

https://fastapi.tiangolo.com/tutorial/request-files/