pl-file-download元件¶
提供静态或动态生成的文件的下载链接。
样品元素¶

question.html
<!-- allow students to download an existing file -->
<pl-file-download file-name="data.txt" directory="clientFilesCourse"></pl-file-download>
<!-- allow students to download a file that is generated by code -->
<pl-file-download file-name="data.txt" type="dynamic"></pl-file-download>
<!-- allow students to open an existing file in a new tab -->
<pl-file-download
file-name="data.txt"
directory="clientFilesCourse"
force-download="false"
></pl-file-download>
定制¶
| 属性 | 类型 | 默认 | 描述 |
|---|---|---|---|
directory |
字符串 | "clientFilesQuestion" |
包含文件的目录,"clientFilesQuestion" 或 clientFilesCourse(请参阅客户端和服务器文件)。如果是 type="dynamic",则无法指定目录。 |
file-name |
字符串 | — | 要下载的文件的名称。 |
force-download |
布尔 | 真实 | 强制下载文件。否则,允许浏览器在新选项卡中打开文件。 |
label |
字符串 | 查看描述 | 下载链接上显示的标签。默认为 file-name 中指定的文件名。如果显式设置,则可以包含 HTML,但必须正确转义代码(例如,对于 <strong>Download</strong> File 的标签使用 label="<strong>Download</strong> File")。 |
type |
"static" 或 "dynamic" |
"static" |
文件类型,"static"(现有文件)或 "dynamic"(由元素或服务器代码生成的文件)。 |
细节¶
如果是 type="dynamic",则文件的内容必须由位于元素代码或 server.py 中的函数 file() 返回。内容必须是字符串(使用 utf-8 编码)、类字节对象或类文件对象。此函数可用的文件名为 data["filename"]。例如,此代码可能出现在 server.py 中以生成名为 data.txt 的文件:
server.py
def file(data):
if data["filename"] == "data.txt":
return "This data is generated by code."
如果 file() 不返回任何内容,则将被视为 file() 返回空字符串。
示例实现¶
- [元素/文件下载]