# 上传图片到插件/主题的最佳方式？

**URL:** <https://meta.discourse.org/t/best-way-to-upload-images-to-a-plugin-theme/360581>\
**Category:** Development\
**Tags:** plugin-api\
**Created:** [2025年四月6日 02:39 UTC](https://meta.discourse.org/t/best-way-to-upload-images-to-a-plugin-theme/360581 "2025-04-06T02:39:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bitmage](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bitmage/32/389881_2.png) [@bitmage](https://meta.discourse.org/u/bitmage)\
**Post date:** [2025年四月6日 02:39 UTC](https://meta.discourse.org/t/best-way-to-upload-images-to-a-plugin-theme/360581/1 "2025-04-06T02:39:10Z")

</div>

我希望能够上传图像图标作为我的 **主题或插件** （我两者都使用）的一部分。然后，我将从插件中的 Glimmer 组件引用它们。我需要能够获取图像位置的字符串 URL，因为我将在 AJAX 请求中将其发送到另一个服务器。（如果您想知道，远程服务器会将图标嵌入到 QR 代码中。）

如果我将图像放在 **主题** 的 `about.json` 中，它们不会被放置在确定性的位置——相反，URL 中有一个哈希。因此，我不知道如何在 CSS 之外引用它们，更不用说在插件中引用了。

如果我将图像作为 `uploaded_image_list` 放在 **插件** 的管理设置中，位置也是不确定的，而且它们最终会出现在一个数组中，没有人类可读的名称/标签/别名来引用它们。

```plaintext
// console.log(this.siteSettings.county_fence_available_icons):
/uploads/default/original/1X/1764ca1c70c7f4f7f01da26e702639fd8cfe2bf4.jpeg|/uploads/default/original/1X/b16cecd70e4d25abdf06a8b6135ec126d2c6ce2d.jpeg|/uploads/default/original/1X/b4c049256c1068e531e43f1622ada78c5b2070b3.jpeg|/uploads/default/original/1X/4aa457c9cd939e178eb251682a4438cc027b96e9.jpeg

```

那么，有没有办法将图像上传到主题或插件，使其最终具有确定的 URL？

这个问题也涉及到一个人想要上传的任何其他资产。`about.json` 的 `assets` 部分以及 `plugin.rb` 中要使用的 `register_asset` 函数_看起来_都能够处理字体以外的其他文件类型，但如果这些文件被上传了，我不知道它们在哪里。

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [2025年四月6日 21:31 UTC](https://meta.discourse.org/t/best-way-to-upload-images-to-a-plugin-theme/360581/2 "2025-04-06T21:31:54Z")

</div>

> [@bitmage](#):
>
> 那么，我是否可以将图片上传到主题或插件，让它们最终出现在一个确定的URL上？

不可以。您是通过变量名来引用它们，就像您刚才做的那样吗？所有上传的文件都将以其内容的哈希值形式保存。

> [@Include assets (e.g. images, fonts) in themes and components](https://meta.discourse.org/t/include-assets-e-g-images-fonts-in-themes-and-components/62459):
>
> Themes and theme components allow you to handle uploaded assets such as images and fonts. You can control what assets your theme accepts using the site setting: theme authorized extensions Including assets in themes and components For remote themes Remote themes include: Themes and components installed from the Popular list of themes Themes and components installed using the From a git repository method To upload assets to a remote theme or theme component, see [Create and share a font theme …](https://meta.discourse.org/t/create-and-share-a-font-theme-component/62462)

---

<div class="post-metadata">

**Author:** ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)\
**Post date:** [2025年四月6日 21:47 UTC](https://meta.discourse.org/t/best-way-to-upload-images-to-a-plugin-theme/360581/3 "2025-04-06T21:47:54Z")

</div>

> [@bitmage](#):
>
> So is there a way I can upload images, either to the theme or plugin, in a way that they end up at a deterministic URL?

即使您无法_设置_URL，它也是_确定性的_，因为URL由文件的SHA1哈希组成。

只要您不更改文件内容，它应该会出现在以下任一位置：

```plaintext
/uploads/<实例名称>/original/1X/1764ca1c70c7f4f7f01da26e702639fd8cfe2bf4.jpeg

```

或者

```plaintext
/uploads/<实例名称>/original/2X/1/1764ca1c70c7f4f7f01da26e702639fd8cfe2bf4.jpeg

```

或者

```plaintext
/uploads/<实例名称>/original/3X/1/7/1764ca1c70c7f4f7f01da26e702639fd8cfe2bf4.jpeg

```

或者

```plaintext
/uploads/<实例名称>/original/4X/1/7/6/1764ca1c70c7f4f7f01da26e702639fd8cfe2bf4.jpeg

```

（具体是哪一个取决于您已有的上传数量（[https://github.com/discourse/discourse/blob/main/lib/file\_store/base\_store.rb#L230C1-L234C8](https://github.com/discourse/discourse/blob/main/lib/file_store/base_store.rb#L230C1-L234C8)）。

---

<div class="post-metadata">

**Author:** ![Alteras](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alteras/32/179824_2.png) [@Alteras](https://meta.discourse.org/u/Alteras)\
**Post date:** [2025年四月6日 21:48 UTC](https://meta.discourse.org/t/best-way-to-upload-images-to-a-plugin-theme/360581/4 "2025-04-06T21:48:21Z")

</div>

至于插件，硬资产（无法通过管理员配置修改的资产）将具有清晰的 URL。

您可以参考 Discourse Math 插件的初始化程序：

> <https://github.com/discourse/discourse-math/blob/main/assets/javascripts/initializers/discourse-math-katex.js>

`/public` 目录中的资产将以插件名称提供。这些资产可以直接调用，无需在 `plugin.rb` 中注册。

---

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [2025年五月6日 21:49 UTC](https://meta.discourse.org/t/best-way-to-upload-images-to-a-plugin-theme/360581/5 "2025-05-06T21:49:19Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
