跳到主内容

将文件存储在本地文件系统

本指南介绍如何将上传的文件存储在 Casdoor 服务器的文件系统中。Casdoor 将它们存储在 files 目录中。例如,如果 Casdoor 运行在 /home/user/casdoor,则文件位于 /home/user/casdoor/files。


学习目标​

  • 添加本地文件系统存储提供商。
  • 选择路径前缀。

所需条件​

  • Casdoor 管理控制台的访问员权限

添加提供商​

  1. 在 Casdoor 管理控制台中,前往 Identity > Providers 并添加一个提供商。

  2. 将 Category 设置为 Storage,将 Type 设置为 Local File System。

    Local file system provider

  3. 可选地,设置 Path prefix。Casdoor 随后会将文件存储在具有该名称的子目录中。

  4. 保存提供商.

带有路径前缀时:

Provider with a path prefix

Files stored under the prefix

不带路径前缀时:

Provider without a path prefix

Files stored without a prefix

备注

在容器中,将 files 目录挂载到卷上。否则,当容器被替换时,文件将会丢失。

文件如何提供服务​

Casdoor 在路径 /files/ 下提供此提供商的文件,例如 https://<your-casdoor-host>/files/<path-prefix>/<file-name>。为了防止上传的文件在 Casdoor 域名上执行脚本,它会向存在于该处的每个文件发送以下响应标头:

HeaderEffect
X-Content-Type-Options: nosniffThe browser doesn't guess another content type from the content of the file
Content-Security-Policy: default-src 'none'; sandboxA file that the browser opens directly loads nothing and runs no script
Content-Disposition: attachmentOnly for active content: the browser downloads the file instead of showing it. Active content is a file with the extension .htm, .html, .mht, .mhtml, .svg, .svgz, .swf, .xht, .xhtml, .xml, .xsl, or .xslt, without regard to case

<img> 标签中的图片(如头像)仍然会显示,因为浏览器在这些位置会忽略这些标头。不存在于 /files/ 下的路径将返回 Casdoor Web 应用程序及其常规标头。

另请参阅​