Skip to content

Commit b019017

Browse files
committed
Add file sharing from path example
Introduces a new function to share files from a file path, including UI updates to provide separate buttons for sharing files from bytes and from paths. File sharing from paths is disabled on the web platform.
1 parent aa32c29 commit b019017

File tree

1 file changed

+28
-2
lines changed
  • sdk/python/examples/services/share

1 file changed

+28
-2
lines changed

sdk/python/examples/services/share/basic.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
import flet as ft
24

35

@@ -21,7 +23,7 @@ async def do_share_uri():
2123
status.value = f"Share status: {result.status}"
2224
result_raw.value = f"Raw: {result.raw}"
2325

24-
async def do_share_files():
26+
async def do_share_files_from_bytes():
2527
file = ft.ShareFile.from_bytes(
2628
b"Sample content from memory",
2729
mime_type="text/plain",
@@ -34,6 +36,23 @@ async def do_share_files():
3436
status.value = f"Share status: {result.status}"
3537
result_raw.value = f"Raw: {result.raw}"
3638

39+
async def do_share_files_from_paths():
40+
if page.web:
41+
status.value = "File sharing from paths is not supported on the web."
42+
return
43+
#
44+
temp_dir = await ft.StoragePaths().get_temporary_directory()
45+
file_path = os.path.join(temp_dir, "sample_from_path.txt")
46+
with open(file_path, "wb") as f:
47+
f.write(b"Sample content from file path")
48+
49+
result = await share.share_files(
50+
[ft.ShareFile.from_path(file_path)],
51+
text="Sharing a file from memory",
52+
)
53+
status.value = f"Share status: {result.status}"
54+
result_raw.value = f"Raw: {result.raw}"
55+
3756
page.add(
3857
ft.SafeArea(
3958
ft.Column(
@@ -42,7 +61,14 @@ async def do_share_files():
4261
[
4362
ft.Button("Share text", on_click=do_share_text),
4463
ft.Button("Share link", on_click=do_share_uri),
45-
ft.Button("Share file", on_click=do_share_files),
64+
ft.Button(
65+
"Share file from bytes",
66+
on_click=do_share_files_from_bytes,
67+
),
68+
ft.Button(
69+
"Share file from path",
70+
on_click=do_share_files_from_paths,
71+
),
4672
],
4773
wrap=True,
4874
),

0 commit comments

Comments
 (0)