如何解决Strapi 上传的文件未显示在网站上
我有很多文件存储在 mongodb 的 upload_file 集合中,它们与相关的内容类型有关系。但是,当我打开 Strapi CMS UI 时,我看不到附加在其内容类型上的文件。
我使用的是 Strapi v3.4.6 — 社区版。
在第一张图片中显示了我的一个upload_file 集合项目。其关系用红圈表示。
第二张图显示了我的主要内容类型集合项。您会看到它的 id 和 upload_file rel id 匹配。
但在 Strapi UI 中,此文件未链接到模型。该文件存在于 Strapi 的文件系统中。然而它是不可见的
解决方法
您需要迁移数据库。我们用一个基本的脚本解决了。
运行 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 举报,一经查实,本站将立刻删除。