在回送组件存储中使用远程挂钩上载之前重命名文件 [英] Rename file before upload using remote hooks in loopback component storage
问题描述
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屋!