我正在<input type="file" name="myfile" id="myfile"
使用sails 从浏览器上传文件。我需要将其放置在默认快递位置以外的位置。我会在裸快车中使用以下代码:
app.use(express.bodyParser({ keepExtensions: true, uploadDir: uploadFolder }))
对于风帆,我按照这个相关答案的方式编写了代码
首先,我尝试了 SkyTecLabs 的回答。策略/configfileupload.js 包含
'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 包含
'SchedController' : {
'uploadsubmit': 'configfileupload'
}
Express 继续将文件上传到默认目录。typeof req, res, next,sailsexpress 是:object, object, function, function 所以签名看起来没问题。(我尝试返回函数 configFileUpload 以防万一,但从未调用过控制器。)
然后我尝试了 mikemcneil 的建议。配置/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 }));
}
};
并且上传仍然放置在默认目录中。Sailsexpress 和 app 的 typeofs 都是函数。
如果上面的代码对于在sails中包含express中间件是正确的,那么也许这个中间件只有在express强大的依赖解析数据并上传文件之后才会执行。
在这种情况下,我不知道在 Sails 中的任何地方可以放置诸如 bodyParser 之类的快速配置选项。