DEV Community

John Ding
John Ding

Posted on

How to place a file generated from strapi backend js code

const mime = require('mime-types'); //used to detect file's mime type
const fs = require('fs');
const rootDir = process.cwd();
const fileName = 'test.csv';
const filePath =
${rootDir}/public/uploads/${fileName}`
const stats = fs.statSync(filePath);

//uploading it directly to upload services.
await strapi.plugins.upload.services.upload.upload({
data:{}, //mandatory declare the data(can be empty), otherwise it will give you an undefined error.
files: {
path: filePath,
name: fileName,
type: mime.lookup(filePath), // mime type of the file
size: stats.size,
},
});`

https://forum.strapi.io/t/strapi-upload-plugin-how-to-place-a-file-generated-from-strapi-backend-js-code/3323

Top comments (0)