How do you specify file upload directory for express within sails?
I'm uploading a file from a browser <input type="file" name="myfile"
id="myfile" using sails. I need to place it in other than the default
express location. I'd use the following code in naked Express:
app.use(express.bodyParser({ keepExtensions: true, uploadDir: uploadFolder
}))
For sails I wrote code along the lines of this relevant answer
First, I tried SkyTecLabs' answer. policies/configfileupload.js contains
'use strict';
var sailsExpress = require('../../node_modules/sails/node_modules/express'),
path = require('path');
console.log('.. initializing policies/configFileUpload');
module.exports = function configFileUpload (req, res, next) {
var uploadFolder = path.normalize(__dirname + '/../public/uploads');
console.log('.. in policies/configfileupload.js. uploadFolder=',
uploadFolder);
console.log('typeofs are=',typeof req, typeof res, typeof next, typeof
sailsExpress);
sailsExpress.bodyParser({ keepExtensions: true, uploadDir: uploadFolder });
next();
};
config/policies.js contains
'SchedController' : {
'uploadsubmit': 'configfileupload'
}
Express continues to upload the file to the default directory. Typeof req,
res, next, sailsexpress are: object, object, function, function so the
signature looks OK. (I tried returning the function configFileUpload just
in case, but the controller was never called.)
Then I tried mikemcneil's suggestion. config/express.js
'use strict';
var sailsExpress = require('../node_modules/sails/node_modules/express'),
path = require('path');
module.exports.express = {
customMiddleware: function (app) {
var uploadFolder = path.normalize(__dirname + '/../public/uploads');
console.log('.. in config/express.js. uploadFolder=', uploadFolder);
console.log('typeof sailsExpress=', typeof sailsExpress, 'typeof
app=', typeof app);
app.use(sailsExpress.bodyParser({ keepExtensions: true, uploadDir:
uploadFolder }));
}
};
and the upload was still placed in the default directory. The typeofs
sailsexpress and app are both function.
Any suggestions?
No comments:
Post a Comment