Strapi:将图像上传到Strapi,图像不会渲染

发布于 2025-02-02 12:08:20 字数 1194 浏览 5 评论 0原文

问题

我想知道您是否可以帮助确定与将图像上传到thumb属于content-type 目录的问题。

图像图像在Strapi面板中呈现后不会呈现,它仅显示名称。

const getFileSize = (filePath) => {
  return statSync(filePath)["size"]; // statSync from `fs-extra` library
};


 strapi.service("api::directory.directory").create({
      data: {},
      files: {
        thumb: {
          path: `${imagesDir}/Crab-1-1-scaled.jpg`,
          name: "Crab-1-1-scaled.jpeg",
          type: mime.lookup(`${imagesDir}/Crab-1-1-scaled.jpg`), // mime from `mime-types` library
          size: getFileSize(`${imagesDir}/Crab-1-1-scaled.jpg`),
        },
      },
    });

console.log

size在上传数据时始终为零

{
  files: {
    thumb: {
      path: 'src/scripts/Directory/images/Crab-1-1-scaled.jpg',
      name: 'Crab-1-1-scaled.jpeg',
      type: 'image/jpeg',
      size: 0
    }
  }
}

输出

,图像未渲染,仅显示图像的名称。

Problem

I would like to know if you can help identify the issue related to uploading an image to the thumb field belonging to the content-type Directory.

The image image does not render once it is in the strapi panel, it only shows the name.

const getFileSize = (filePath) => {
  return statSync(filePath)["size"]; // statSync from `fs-extra` library
};


 strapi.service("api::directory.directory").create({
      data: {},
      files: {
        thumb: {
          path: `${imagesDir}/Crab-1-1-scaled.jpg`,
          name: "Crab-1-1-scaled.jpeg",
          type: mime.lookup(`${imagesDir}/Crab-1-1-scaled.jpg`), // mime from `mime-types` library
          size: getFileSize(`${imagesDir}/Crab-1-1-scaled.jpg`),
        },
      },
    });

console.log

size is always zero

{
  files: {
    thumb: {
      path: 'src/scripts/Directory/images/Crab-1-1-scaled.jpg',
      name: 'Crab-1-1-scaled.jpeg',
      type: 'image/jpeg',
      size: 0
    }
  }
}

output

When uploading the data the image is not rendered, it only shows the name of the image.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你げ笑在眉眼 2025-02-09 12:08:20

我在Strapi 4中所做的是:我首先使用内置上传插件,该插件返回上传文件的ID。然后,我使用ID创建/更新我的实体(在您的情况下 Directory )。

这是您可以使用的工作代码段:

    const filename = originalFile.split('.')[0]
    const ext = originalFile.split('.').pop()
    const rootDir = process.cwd();
    const filePath = `${rootDir}/public/uploads/${originalFile}` // the file is moved here already - the upload plugin will not upload anything, only reference the existing file.

    const entity = {
      path: filePath,
      name: originalFile,
      hash: `${filename}${ext}`,
      mime: mime.lookup(filePath),
      provider: 'local',
      ext: ext,
      type: mime.lookup(filePath),
      size: fs.statSync(filePath)["size"],
    };

    const localUpload = await strapi.plugin('upload').service('upload').upload({
      files: entity,
      data: {}
    });
    console.log("LOCAL UPLOAD done", localUpload[0].id)

What I do in Strapi 4 is this: I first use the built-in upload plugin, which returns an id of the uploaded file. I then use the id to create/update my entity (Directory in your case).

Here is a working code snippet you can use:

    const filename = originalFile.split('.')[0]
    const ext = originalFile.split('.').pop()
    const rootDir = process.cwd();
    const filePath = `${rootDir}/public/uploads/${originalFile}` // the file is moved here already - the upload plugin will not upload anything, only reference the existing file.

    const entity = {
      path: filePath,
      name: originalFile,
      hash: `${filename}${ext}`,
      mime: mime.lookup(filePath),
      provider: 'local',
      ext: ext,
      type: mime.lookup(filePath),
      size: fs.statSync(filePath)["size"],
    };

    const localUpload = await strapi.plugin('upload').service('upload').upload({
      files: entity,
      data: {}
    });
    console.log("LOCAL UPLOAD done", localUpload[0].id)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文