猿问

如何从trapi的上传文件夹中删除文件?

我有一个带有媒体类型字段的条目。


当我尝试删除某些条目时,我使用的是代码:


strapi.query('entry').delete({ id: entry.id });

strapi.query('file', 'upload').delete({ id: entry.image.id });

该条目已成功删除,以及“文件上传”中的记录。但文件仍保留在上传文件夹中。我怎样才能删除它?


人到中年有点甜
浏览 251回答 3
3回答

紫衣仙女

此代码从媒体库中删除条目并从uploads目录中删除文件:const file = await strapi.plugins['upload'].services.upload.fetch({ id });await strapi.plugins['upload'].services.upload.remove(file);

慕娘9325324

假设您的内容类型是文章,请尝试以下操作:在/api/articles/controllers/articles.js 'use strict';const { parseMultipartData, sanitizeEntity } = require('strapi-utils');module.exports = {  async delete(ctx) {    const { id } = ctx.params;    const entity = await strapi.services.articles.delete({ id });    //with strapi multiple media    if(entity){      if (entity.gallery.length > 0) {        entity.gallery.forEach(image => {          strapi.plugins.upload.services.upload.remove(image);        });      }    }    //or with strapi single media    if(entity){      strapi.plugins.upload.services.upload.remove(entity.image);    }    return sanitizeEntity(entity, { model: strapi.models.articles });  }}

有只小跳蛙

尝试调用 Strapi org. 控制器中的功能:delete: async ctx => {    ctx.body = await strapi.plugins['content-manager'].services['contentmanager'].delete(ctx.params, ctx.request.query);},deleteAll: async ctx => {    ctx.body = await strapi.plugins['content-manager'].services['contentmanager'].deleteMany(ctx.params, ctx.request.query);}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答