node.js - 环回: Rename file from before remote method using context

标签 node.js loopbackjs loopback angular-loopback

我想使用上下文重命名远程 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/

相关文章:

node.js - 从启动脚本环回注入(inject)新模型

loopback - 使用 lb4 搭建新的后端应用程序

c# - Socket.Bind 然后连接到 MS Loopback NIC

node.js - NodeJS 应用程序之间是否存在 session ?

jquery - 有没有办法选择 cheerio 中的每个元素?

jquery - 环回: POST an array of objects through ajax

javascript - 自定义环回模型

arrays - 过滤环回中字段的数组类型

node.js - 如何更新 Mongo 集合中的部分?

node.js - 如何使用 Node.js 中的层在 AWS SAM 模板中的 lambdas 中使用共享代码?