我想使用上下文重命名远程 Hook 之前的文件。
container.beforeRemote('upload', function (context, res, next) {
/////rename file
}
谁能告诉我如何从中访问文件?
最佳答案
我不知道以前是否可以这样做,因为我们还没有解压多部分表单。
afterRemote
Hook 包含足够的信息来重命名文件(如果您确实需要的话)。这是一个基于环回的 default storage example 构建的示例
app.start = function() {
// Adding an operation hook which renames the recently uploaded file
var container = app.dataSources.storage.models.container;
container.afterRemote('upload', (context, res, next) => {
// The file object is stored in the res param
let file = res.result.files.file[0];
// Get the filepath of our datasource, in this case `storage`.
let root = container.dataSource.settings.root;
// Get the full path of the file we just uploaded
// root/containerName/filename.ext
let filePath = path.resolve(root, file.container, file.name);
// aand rename
fs.rename(filePath, path.resolve(root, file.container, 'newFile.txt'), () => console.log('renamed!'));
});
return app.listen(function() {
app.emit('started');
var baseUrl = app.get('url').replace(/\/$/, '');
console.log('Web server listening at: %s', baseUrl);
if (app.get('loopback-component-explorer')) {
var explorerPath = app.get('loopback-component-explorer').mountPath;
console.log('Browse your REST API at %s%s', baseUrl, explorerPath);
}
});
};
关于node.js - 环回: Rename file from before remote method using context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49432532/