在回送组件存储中使用远程挂钩上载之前重命名文件 [英] Rename file before upload using remote hooks in loopback component storage

查看:178
本文介绍了在回送组件存储中使用远程挂钩上载之前重命名文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在上传回送组件存储器之前,我很难重命名一个文件。看起来,loopback并没有提供相同的内置选项。对于从一个角度的形式上传,我已经使用了角度uploader beforeupload方法来改变文件名使用以下方法:

  this.fileExtension ='。'+ item.file.name.split('。')。pop(); 
item.file.name = Math.random()。toString(36).substring(7)+ new Date()。getTime()+ this.fileExtension;

是否可以在回送组件存储中的上传方法远程挂接之前执行相同的操作?我的意图是对来自移动设备的api请求执行相同的文件名更改操作。如果一个远程钩子不能做同样的事情,有没有其他的方法来达到相同的结果?提前致谢!

解决方案

假如您有存储 > datasources.json 。



您可以在启动脚本中执行:

  // server / boot / any.js 
module.exports = function(app){
app.dataSources.storage.connector.getFilename = function(file, req,res){
//file.name是上传的原始文件名
var filename = req.query.filename || general.ext;
返回文件名;
}
};

并在上传网址中添加文件名

例如: /containers/my-container/upload?filename=profile.jpg


I am having difficulty in renaming a file before upload in loopback component storage. As it seems, loopback does'nt provide a built-in option for the same. For uploading from a angular form, I have used the angular uploader beforeupload method to change the filename using following method:

this.fileExtension = '.' + item.file.name.split('.').pop();
item.file.name = Math.random().toString(36).substring(7) + new Date().getTime() + this.fileExtension;

Is it possible to perform the same operations in before remote hook of the upload method in loopback component storage? My intention is to do the same file name change operation for api requests coming from mobile devices. If a remote hook cannot do the same, is there any other method for achieving the same result? Thanks in advance!

解决方案

Say you have storage DS defined in datasources.json.

You can do it in a boot script :

//server/boot/any.js
module.exports = function(app){
app.dataSources.storage.connector.getFilename = function (file, req, res) {
  //file.name is original filename uploaded
  var filename = req.query.filename || 'general.ext';
  return filename;
}
};

and add filename in the upload url.

e.g : /containers/my-container/upload?filename=profile.jpg

这篇关于在回送组件存储中使用远程挂钩上载之前重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆