Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $('#upload-input').on('change', function () {
- var files = $(this).get(0).files;
- if (files.length > 0) {
- // One or more files selected, process the file upload
- var form = new FormData();
- for (var index = 0; index < files.length; index++) {
- var file = files[index];
- form.append('Uploded Files', file, file.name);
- }
- $.ajax({
- url: 'api/fileupload/upload',
- type: 'POST',
- data: form,
- processData: false,
- contentType: false,
- success: function (data) {
- console.log('upload successful!');
- }
- });
- }
- });
- FileUpload.remoteMethod
- (
- 'upload', {
- http: {
- verb: 'post',
- },
- accepts:
- [
- { arg: 'ctx', type: 'object', http: { source: 'context' } },
- { arg: 'options', type: 'object', http: { source: 'query' } }
- ],
- returns: {
- arg: 'data',
- type: 'string',
- root: true
- }
- }
- );
- FileUpload.upload = function (context, options, callback) {
- //context.req.params = 'common';
- };
- npm install --save multer
- 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);
- }
- 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'
- }
- });
- };
- FileUpload.remoteMethod(
- 'upload', {
- http: {
- verb: 'post',
- },
- accepts:
- [{
- arg: 'req',
- type: 'object',
- http: {
- source: 'req'
- }
- }, {
- arg: 'res',
- type: 'object',
- http: {
- source: 'res'
- }
- }],
- returns: {
- arg: 'data',
- type: 'string',
- root: true
- }
- }
- );
- 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
- console.log("----------Second Rakesh---");
- console.log(file);
- var ext = file.originalname.substring(file.originalname.lastIndexOf("."));
- var fileName = Date.now() + ext;
- uploadedFileName = fileName;
- cb(null, fileName);
- }
- });
- FileUpload.upload = function (req, res, callback) {
- var upload = multer({
- storage: storage
- }).array('file', 12);
- upload(req, res, function (err) {
- if (err) {
- // An error occurred when uploading
- res.json(err);
- }
- console.log("-------------Rakesh"); // Its Printing Rakesh
- res.json(uploadedFileName);
- });
- };
- $ lb datasource
- [?] Enter the data-source name: myfilesystem
- [?] Select the connector for myfilesystem: other
- [?] Enter the connector name: loopback-component-storage
- [?] Install storage (Y/n)
Add Comment
Please, Sign In to add comment