Skip to content

Commit af880fc

Browse files
committed
Added File.content_bytes
1 parent ccef0a8 commit af880fc

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

adafruit_httpserver/request.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,25 @@ def __init__(
9494
self.content_type = content_type
9595
self.content = content
9696

97+
@property
98+
def content_bytes(self) -> bytes:
99+
"""
100+
Content of the file as bytes.
101+
It is recommended to use this instead of ``content`` as it will always return bytes.
102+
103+
Example::
104+
105+
file = request.form_data.files.get("uploaded_file")
106+
107+
with open(file.filename, "wb") as f:
108+
f.write(file.content_bytes)
109+
"""
110+
return (
111+
self.content.encode("utf-8")
112+
if isinstance(self.content, str)
113+
else self.content
114+
)
115+
97116
@property
98117
def size(self) -> int:
99118
"""Length of the file content."""

0 commit comments

Comments
 (0)