浏览代码

Merge pull request #2494 from zauberzeug/download-media-type

Introduce media_type parameter for `ui.download`
Falko Schindler 1 年之前
父节点
当前提交
194a64a6f5
共有 3 个文件被更改,包括 8 次插入7 次删除
  1. 2 2
      nicegui/client.py
  2. 3 2
      nicegui/functions/download.py
  3. 3 3
      nicegui/templates/index.html

+ 2 - 2
nicegui/client.py

@@ -219,9 +219,9 @@ class Client:
         path = target if isinstance(target, str) else self.page_routes[target]
         path = target if isinstance(target, str) else self.page_routes[target]
         self.outbox.enqueue_message('open', {'path': path, 'new_tab': new_tab}, self.id)
         self.outbox.enqueue_message('open', {'path': path, 'new_tab': new_tab}, self.id)
 
 
-    def download(self, src: Union[str, bytes], filename: Optional[str] = None) -> None:
+    def download(self, src: Union[str, bytes], filename: Optional[str] = None, media_type: str = '') -> None:
         """Download a file from a given URL or raw bytes."""
         """Download a file from a given URL or raw bytes."""
-        self.outbox.enqueue_message('download', {'src': src, 'filename': filename}, self.id)
+        self.outbox.enqueue_message('download', {'src': src, 'filename': filename, 'media_type': media_type}, self.id)
 
 
     def on_connect(self, handler: Union[Callable[..., Any], Awaitable]) -> None:
     def on_connect(self, handler: Union[Callable[..., Any], Awaitable]) -> None:
         """Register a callback to be called when the client connects."""
         """Register a callback to be called when the client connects."""

+ 3 - 2
nicegui/functions/download.py

@@ -4,17 +4,18 @@ from typing import Optional, Union
 from .. import context, core, helpers
 from .. import context, core, helpers
 
 
 
 
-def download(src: Union[str, Path, bytes], filename: Optional[str] = None) -> None:
+def download(src: Union[str, Path, bytes], filename: Optional[str] = None, media_type: str = '') -> None:
     """Download
     """Download
 
 
     Function to trigger the download of a file, URL or bytes.
     Function to trigger the download of a file, URL or bytes.
 
 
     :param src: target URL, local path of a file or raw data which should be downloaded
     :param src: target URL, local path of a file or raw data which should be downloaded
     :param filename: name of the file to download (default: name of the file on the server)
     :param filename: name of the file to download (default: name of the file on the server)
+    :param media_type: media type of the file to download (default: "")
     """
     """
     if not isinstance(src, bytes):
     if not isinstance(src, bytes):
         if helpers.is_file(src):
         if helpers.is_file(src):
             src = core.app.add_static_file(local_file=src, single_use=True)
             src = core.app.add_static_file(local_file=src, single_use=True)
         else:
         else:
             src = str(src)
             src = str(src)
-    context.get_client().download(src, filename)
+    context.get_client().download(src, filename, media_type)

+ 3 - 3
nicegui/templates/index.html

@@ -212,13 +212,13 @@
         });
         });
       }
       }
 
 
-      function download(src, filename) {
+      function download(src, filename, mediaType) {
         const anchor = document.createElement("a");
         const anchor = document.createElement("a");
         if (typeof src === "string") {
         if (typeof src === "string") {
           anchor.href = src.startsWith("/") ? "{{ prefix | safe }}" + src : src;
           anchor.href = src.startsWith("/") ? "{{ prefix | safe }}" + src : src;
         }
         }
         else {
         else {
-          anchor.href = URL.createObjectURL(new Blob([src]))
+          anchor.href = URL.createObjectURL(new Blob([src], {type: mediaType}))
         }
         }
         anchor.target = "_blank";
         anchor.target = "_blank";
         anchor.download = filename || "";
         anchor.download = filename || "";
@@ -307,7 +307,7 @@
               const target = msg.new_tab ? '_blank' : '_self';
               const target = msg.new_tab ? '_blank' : '_self';
               window.open(url, target);
               window.open(url, target);
             },
             },
-            download: (msg) => download(msg.src, msg.filename),
+            download: (msg) => download(msg.src, msg.filename, msg.media_type),
             notify: (msg) => Quasar.Notify.create(msg),
             notify: (msg) => Quasar.Notify.create(msg),
           };
           };
           const socketMessageQueue = [];
           const socketMessageQueue = [];