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 :)
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) {
...
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With