我正在使用sails并将文件从客户端上传到服务器而没有表单,我正在使用fs.createReadStream('file').我将文件数据发送到服务器,当我上传文件时,我获得了所有数据,但更改了文件名并设置了随机文件名.以下是我的代码.
var uploadFile = req.file('file');
uploadFile.upload({
maxBytes: 250000000000,
dirname: '../../assets/videos'
}, function onUploadComplete(err, files) {
if (err) {
return res.json({
status: false,
msg: 'uploading error'
}); // False for err
} else {
return res.json({
status: true,
msg: 'success',
data: files
}); // True for success
}
});
Run Code Online (Sandbox Code Playgroud)
在文件中我得到了一个有效的数组,但我不知道它为什么会改变文件名.上传时是否有设置自定义名称的选项?如:
var object = {
maxBytes: 250000000000,
dirname: '../../assets/videos',
fileName: 'xyz.mp4'
}
Run Code Online (Sandbox Code Playgroud)
提前致谢 :)
MjZ*_*Zac 12
您可以通过传递saveAs选项保存具有自定义名称的文件.(假设您使用的是默认值sails-skipper)
uploadFile.upload({
dirname: 'path to store the file',/* optional. defaults to assets/uploads I guess*/
saveAs: 'new file name', /* optional. default file name. Can also take a function */
maxBytes: 5 * 1024 * 1024 //5 MB
},function onUploadComplete(err, uploadedFiles) {
...
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2179 次 |
| 最近记录: |