Upload file from sftp to upload folder and use it on strapi admin

To upload a file from your machine/server and to avoid REST requests, please take a look at the following example:

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.getType(filePath), // mime type of the file
		size: stats.size,
	},
});

Take a look that I uses a third party library called mime-types. That one is already installed in your project as it is used by Strapi.

2 Likes