I created a simple file uploading application with loopbackjs. In the application\'s client side I used simple HTML and Javascript code, calling a loopback api with an AJAX
Install multer : https://github.com/expressjs/multer
npm install --save multer
In MyModel.js
var multer = require('multer');
var fs = require('fs');
module.exports = function (MyModel) {
var uploadedFileName = '';
var storage = multer.diskStorage({
destination: function (req, file, cb) {
// checking and creating uploads folder where files will be uploaded
var dirPath = 'client/uploads/'
if (!fs.existsSync(dirPath)) {
var dir = fs.mkdirSync(dirPath);
}
cb(null, dirPath + '/');
},
filename: function (req, file, cb) {
// file will be accessible in `file` variable
var ext = file.originalname.substring(file.originalname.lastIndexOf("."));
var fileName = Date.now() + ext;
uploadedFileName = fileName;
cb(null, fileName);
}
});
MyModel.upload = function (req, res, cb) {
var upload = multer({
storage: storage
}).array('file', 12);
upload(req, res, function (err) {
if (err) {
// An error occurred when uploading
res.json(err);
} else {
res.json(uploadedFileName);
}
});
};
MyModel.remoteMethod('upload', {
accepts: [{
arg: 'req',
type: 'object',
http: {
source: 'req'
}
}, {
arg: 'res',
type: 'object',
http: {
source: 'res'
}
}],
returns: {
arg: 'result',
type: 'string'
}
});
};
Frontend - I use AngularJS, so for that follow -https://github.com/nervgh/angular-file-upload
there are also other such directives to use
P.S. - As per your comment - Actually Our Problem is that , we need to upload a File from Client Side and Store this File in Database , But Before save in DB , we need to get files on Server side , if we get Files on Server side via Post API than we can easily store file in DB
Can't provide you exact solution, but using above method files will be uploaded in your /client/uploads folder, once uploaded then you have control on what to do with file and once everything is done, eventually delete it(optional)