async def home(): return file( get_example_css, "text/css", file_name="home.css", content_disposition=ContentDispositionType.INLINE, )
async def home(): return file( EXAMPLE_HTML.encode("utf8"), "text/css", file_name="home.css", content_disposition=ContentDispositionType.INLINE, )
async def home(): return file( value, "text/plain", file_name="example.txt", content_disposition=ContentDispositionType.INLINE, )
async def send_file_with_bytes_io(): return file( io.BytesIO(b"some initial binary data: "), "text/plain", file_name="data.txt", content_disposition=ContentDispositionType.INLINE, )
async def send_file_with_async_gen_two(): async def generator(): yield b"Black Knight: None shall pass.\n" yield b"King Arthur: What?\n" yield b"Black Knight: None shall pass.\n" await asyncio.sleep(0.01) yield (b"King Arthur: I have no quarrel with you, good Sir Knight, " b"but I must cross this bridge.\n") yield b"Black Knight: Then you shall die.\n" yield b"King Arthur: I command you, as King of the Britons, to stand aside!\n" await asyncio.sleep(0.01) yield b"Black Knight: I move for no man.\n" yield b"King Arthur: So be it!\n" yield (b"[rounds of melee, with Arthur cutting off the left arm of " b"the black knight.]\n") await asyncio.sleep(0.01) yield b"King Arthur: Now stand aside, worthy adversary.\n" yield b"Black Knight: Tis but a scratch.\n" return file( generator, "text/plain", file_name="black-knight.txt", content_disposition=ContentDispositionType.INLINE, )
async def send_file_with_async_gen(): return file( get_static_path("pexels-photo-923360.jpeg"), "image/jpeg", file_name="nice-cat.jpg", content_disposition=ContentDispositionType.INLINE, )
def file( self, value: Union[ Callable[[], AsyncIterable[bytes]], str, bytes, bytearray, BytesIO ], content_type: str, *, file_name: str = None, content_disposition: ContentDispositionType = ContentDispositionType.ATTACHMENT, ) -> Response: """ Returns a binary file response with given content type and optional file name, for download (attachment) (default HTTP 200 OK). This method supports both call with bytes, or a generator yielding chunks. Remarks: this method does not handle cache, ETag and HTTP 304 Not Modified responses; when handling files it is recommended to handle cache, ETag and Not Modified, according to use case. """ return file( value, content_type, content_disposition=content_disposition, file_name=file_name, )
def test_files_raises_for_invalid_input(): with pytest.raises(ValueError): file(True, "text/plain") # type: ignore with pytest.raises(ValueError): file(100, "text/plain") # type: ignore with pytest.raises(ValueError): file([10, 120, 400], "text/plain") # type: ignore
async def send_file_with_bytes(): def generator(): yield b'Black Knight: None shall pass.\n' yield b'King Arthur: What?\n' yield b'Black Knight: None shall pass.\n' yield b'King Arthur: I have no quarrel with you, good Sir Knight, but I must cross this bridge.\n' yield b'Black Knight: Then you shall die.\n' yield b'King Arthur: I command you, as King of the Britons, to stand aside!\n' yield b'Black Knight: I move for no man.\n' yield b'King Arthur: So be it!\n' yield b'[rounds of melee, with Arthur cutting off the left arm of the black knight.]\n' yield b'King Arthur: Now stand aside, worthy adversary.\n' yield b'Black Knight: Tis but a scratch.\n' all_bytes = b''.join(generator()) return file(all_bytes, 'text/plain', 'black-knight.txt', content_disposition=ContentDispositionType.INLINE)
def test_files_raises_for_invalid_name_with_folder_path(): with pytest.raises(ValueError): file(b"Hello, There!", "text/plain", file_name="not_good/")
async def home(): nonlocal bytes_io bytes_io = BytesIO() bytes_io.write("Żywią i bronią".encode("utf-8")) return file(bytes_io, "text/plain; charset=utf-8", file_name="foo.txt")
async def home(): return file(get_example_css, "text/css")
async def home(): return file(file_path, "text/plain; charset=utf-8", file_name="foo.xml")
async def home(): return file(file_path, "text/plain; charset=utf-8")
async def send_file_with_bytes_io(): return file(io.BytesIO(b"some initial binary data: "), 'text/plain', 'data.txt', content_disposition=ContentDispositionType.INLINE)
async def send_file_with_async_gen(): return file('static/pexels-photo-923360.jpeg', 'image/jpeg', 'nice-cat.jpg', content_disposition=ContentDispositionType.INLINE)