我正在使用Plug.Static从服务器提供静态文件。这样做的“默认”方法是像这样配置它:
plug Plug.Static,
at: "/my_project/assets",
from: :my_project,
gzip: true
然后我可以使用html中的priv / static文件来使用这些文件:
<img class='picture' src='<%= static_path(@conn, "/myPicture.png") %>'>
到目前为止一切顺利。但是,如果我想通过其他路径从priv / static提供文件,则使用
plug Plug.Static,
at: "/my_project/another_path/assets",
from: :my_project,
gzip: true
现在,我无法使用static_path访问文件,因为它仍然解析为host.com/assets/my-picture-hash
而不是host.com/another_path/assets/my-picture-hash
,这是预期的行为。
当散列文件未显示在默认路径中时,如何获取其实际路径?
答案 0 :(得分:0)
您确定第一个示例有效吗?我认为该配置仅在称为static_path(@conn, "/my_project/assets/myPicture.png)
时有效。
因此您的第二个示例在被称为static_path(@conn, "/my_project/another_path/assets/myPicture.png")
似乎您混合使用了:at
和:to
选项。这来自Plug.Static
docs:
:at
-到达静态资产的请求路径。它一定是 字符串。
:from
-从中读取静态资产的文件系统路径。有可能 任一个:包含文件系统路径的字符串,代表的原子 应用程序名称(将从priv/static
提供资产的位置), 或包含应用程序名称和要服务目录的元组 (priv/static
之外的所有资产。
所以要回答您问题的 title (如何从非默认路径提供静态文件?);如果您想从其他路径而不是应用程序文件夹中的默认priv/static
提供文件,则不应该:
plug Plug.Static, at: "/uploads", from: "/some/other/path"
并显示位于/some/other/path/myPicture.png
上的磁盘上的映像(这是绝对路径,而不是相对于您的应用程序):
<img class="picture" src="<%= static_path(@conn, "/uploads/myPicture.png") %>">
((如果仍然无法使用 ,您已将其作为新Plug.Static
之后的默认值添加:放在之前默认的静态插件。)
编辑:
当然,如果您想通过默认路径(应用文件夹中的priv/static
)提供静态资产,但使用自定义网址,则可以执行以下操作:
plug Plug.Static, at: "/some/path", from: :my_project
并将其用作:
<img class="picture" src="<%= static_path(@conn, "/some/path/myPicture.png") %>">