Spaces:
Running
Running
File size: 1,577 Bytes
bd26047 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py
index 7be8f774..8c2cc2a2 100644
--- a/fastapi/dependencies/utils.py
+++ b/fastapi/dependencies/utils.py
@@ -1058,6 +1058,15 @@ def _extract_form_items(
yield key, param_values, None
+async def _read_file_bytes(file: UploadFile) -> bytes:
+ # Automatic conversion must not consume a file shared with other parameters.
+ position = file.file.tell()
+ try:
+ return await file.read()
+ finally:
+ await file.seek(position)
+
+
async def _extract_form_body(
body_fields: list[ModelField],
received_body: FormData,
@@ -1067,7 +1076,7 @@ async def _extract_form_body(
if is_bytes_or_nonable_bytes_annotation(
field.field_info.annotation
) and isinstance(value, UploadFile):
- value = await value.read()
+ value = await _read_file_bytes(value)
elif is_bytes_sequence_annotation(
field.field_info.annotation
) and value_is_sequence(value):
@@ -1075,7 +1084,7 @@ async def _extract_form_body(
results: list[bytes | str] = []
for sub_value in value:
if isinstance(sub_value, UploadFile):
- results.append(await sub_value.read())
+ results.append(await _read_file_bytes(sub_value))
else:
results.append(sub_value)
value = serialize_sequence_value(field=field, value=results)
|