Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails - how to set custom file name req.file('file')

I am working in sails and uploading a file from client to server without form, I am using fs.createReadStream('file'). I sent the file data to server and I got all the data when I upload a file it works but change the file name and set a random file name. Below is my code.

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
     }
   });

In files I got a valid array but I don't know why it changes the file name. Is there any option to set a custom name while uploading ? such as:

var object = {
     maxBytes: 250000000000,
     dirname: '../../assets/videos',
     fileName: 'xyz.mp4'
   }

Thanks in advance :)

like image 437
Shiv Aggarwal Avatar asked Dec 04 '22 00:12

Shiv Aggarwal


1 Answers

You can save a file with a custom name by passing saveAs option. (Assuming you are using default 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) {
    ...
})
like image 85
MjZac Avatar answered Dec 25 '22 15:12

MjZac