微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Strapi 上传的文件未显示在网站上

如何解决Strapi 上传的文件未显示在网站上

我有很多文件存储在 mongodb 的 upload_file 集合中,它们与相关的内容类型有关系。但是,当我打开 Strapi CMS UI 时,我看不到附加在其内容类型上的文件

我使用的是 Strapi v3.4.6 — 社区版。

第一张图片显示了我的一个upload_file 集合项目。其关系用红圈表示。

第二张图显示了我的主要内容类型集合项。您会看到它的 id 和 upload_file rel id 匹配。

但在 Strapi UI 中,此文件链接到模型。该文件存在于 Strapi 的文件系统中。然而它是不可见的

Here is showing the my one of upload_file collection item. Its relation is shown in red circle

Here is showing the my main content type collection item. You see that its id and upload_file rel id is matching

But in Strapi UI,this file is not linked to model. The file exists in file system of Strapi. However it is not visible

我可以手动添加这个文件,但是有什么快速方法吗?

解决方法

您需要迁移数据库。我们用一个基本的脚本解决了。

运行 http://localhost:3000/migrate

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017";
var dbName = "YOURDBNAME";
const express = require('express')
const app = express()
const port = 3000
var _db;
var _dbo;

var tables = {
    "table1": "TabLE1","table2": "TABle2",}

app.get('/migrate',(req,res) => {
  res.send('Started!')
  
  _dbo.collection("upload_file").find({}).toArray(function(err,result) {
    if (err) throw err;
    result.forEach(function (item) {
      if (item.related.length > 0) {
        var related = item.related[0];
        var query = { '_id': related.ref };
        var newvalues = { $set: {} };
        newvalues.$set[related.field] = item._id;
        var tableName = related.kind.toLowerCase();
        _dbo.collection(tables[tableName]).updateOne(query,newvalues,function(err,res) {
          if (err) throw err;
          console.log(res != null ? res.ops : null);
        });
      }
    })
    // db.close();
  });
})


MongoClient.connect(url,db) {
  if (err) throw err;
  _dbo = db.db(dbName);
});

app.listen(port,() => {
  console.log(`Example app listening at http://localhost:${port}`)
})

function capitalizeFirstLetter(string) {
  return string.charAt(0).toUpperCase() + string.slice(1);
}

// Run http://localhost:3000/migrate
,

将图片上传到 Strapi 时,请确保您的 formData 包含这些字段

const formData = new FormData();
formData.append('files',image);    
formData.append('ref','contentTypeName');
formData.append('refId',dataItemId); 
formData.append('field','image'); 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?