-
First Check
Commit to Help
Example Code[Server]
from fastapi import FastAPI, File, UploadFile
app = FastAPI()
@app.post("/single")
async def check(file: bytes = File() ):
print(len(file))
return {"state": 200}
@app.post("/multi")
async def check(files: list[bytes] = File() ):
for file in files:
print(len(file))
return {"state": 200}
[Client]
import requests
files1 = { "file": open("images/4_1_1.jpg", "rb") }
files2 = {
"file1": open("images/4_1_1.jpg", "rb"),
"file2": open("images/4_1_2.jpg", "rb")
}
r = requests.post("http://127.0.0.1:8000/single", files=files1)
result = r.json()
print(result["state"])
r = requests.post("http://127.0.0.1:8000/multi", files=files2)
result = r.json()
print(result["state"])
I have problem about transfer multi files from client to Server..DescriptionSingle file send dont have issue.. result of single file send: result of multi file send: How can i fix it..? please help me Operating SystemmacOS Operating System DetailsNo response FastAPI Version0.78.0 Python Version3.10.4 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 2 replies
-
|
From the request files docs, the multifile upload argument should probably be like this: |
Beta Was this translation helpful? Give feedback.
-
Your code for pytho3.6 and above.. right? |
Beta Was this translation helpful? Give feedback.
-
|
The main reason you have a problem is because you are sending with two different names. Multiple files in FastAPI are received as a I have created a working sample for you here: https://github.com/JarroVGIT/fastapi-github-issues/tree/master/4992 Note that I use The app: from fastapi import FastAPI, File, UploadFile
import uvicorn
app = FastAPI()
@app.post("/multi")
async def check_multi_files(files: list[UploadFile]):
return {"filenames": [file.filename for file in files]}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)The client: import requests
url = 'http://localhost:8000/multi'
multiple_files = [('files', ('file1.txt', open('files/file1.txt', 'rb'), 'text/plain')),
('files', ('file2.txt', open('files/file2.txt', 'rb'), 'text/plain'))]
r = requests.post(url, files=multiple_files)
print(r.text)Hope this clarifies. |
Beta Was this translation helpful? Give feedback.
-
Hi JarroVGIT~!. First of all, Thank you. |
Beta Was this translation helpful? Give feedback.
-
|
I want to create a route to upload multiple files where each file has a name and description field like saving a list of identified documents for a user, how will i write my fastapi route to handle all files and get their names and descriptions correctly; in a json payload it could look like this {
"files":[
{"name": "Residential document": "description":"This is a residential document", "file": "some bytes data"},
{"name": "Residential document": "description":"This is a residential document", "file": "some bytes data"},
{"name": "Residential document": "description":"This is a residential document", "file": "some bytes data"},
]
}I know i can create one route to this this for a single file , but it would mean that the client would need to call my api route multiple times in a row. Is this a good design pattern or is there a better way |
Beta Was this translation helpful? Give feedback.




Hi JarroVGIT~!. First of all, Thank you.
I have coded as per your suggestion and succeeded in uploading multi-files.
Thank you~!