Skip to content
Advertisement

Upload files Sails JS Skipper v0.10.5

I am uploading files with skipper, everything it’s working perfectly, but I have a problem with the option saveAs I am assigning it’s value by means of a function but it doesn’t work, how can I assign the value of req.param(‘titulo’) + file extension to the option saveAs?

var path = require('path');

module.exports = {

'save':function(req,res,next){

    var uploadOptions = {
        dirname: sails.config.appPath + '/assets/books',
        saveAs: function(file){
            return req.param('titulo')+path.extname(file.filename);
        },
        maxBytes: 20 * 1000 * 1000
    }

    req.file('archivoPath').upload(uploadOptions,function(err,files){
        if(err){
            return res.serverError(err);
        }
        else{
            console.log(files);
        }
    });

    Book.create(req.params.all(),function bookCreated(err,book,next){
        if(err) {
            console.log(err);
        }
        return res.redirect('/book/books');
    });
}

};

I also really want to know if inside of the folder assets would be a good place to upload a pdf file to show it in my front end, ty.

Advertisement

Answer

I solved the problem by replacing the saveAs function:

saveAs: function(file){
    return req.param('titulo') + path.extname (file.filename);
},

with the following:

saveAs: function (__newFileStream, cb) {
    cb(null, req.param('titulo') + path.extname(__newFileStream.filename));
},
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement