Git Product home page Git Product logo

Comments (8)

mikermcneil avatar mikermcneil commented on May 24, 2024

https://github.com/felixge/node-formidable

from sails.

mikermcneil avatar mikermcneil commented on May 24, 2024

The formidable bundled with Express does a pretty good job of this, allowing you to access files through req.files. On the front end, I'd recommend using https://github.com/blueimp/jQuery-File-Upload. Mast may include bundled support for this extension down the road as part of Mast.Form.

For now, I'm closing this, but here's an example of how to handle uploads on the backend:

var UUIDGenerator = require('node-uuid');

var DirectoryController = {
    // uploads a file to a directory
    upload: function(req, res) {

        // Parse form data from server
        var parsedFormData = JSON.parse(req.param('data'));

        // Iterate through each uploaded file
        var resultSet= [];
        async.forEach(req.files.files,function(file,cb) {
            async.auto({
                // Calculate new path and filename, using a UUID and the old extension
                metadata: function(cb){
                    var uuid = UUIDGenerator.v1(),
                        newFileName= uuid + "." + _.str.fileExtension(file.name),
                        newPath= config.appPath+"/public/files/"+newFileName;
                    cb(null,{
                        uuid: uuid,
                        newFileName: newFileName,
                        newPath: newPath
                    });
                },
                // Read temp file
                readFile: function(cb,r){   
                    console.log(file.path);
                    fs.readFile(file.path,cb);
                },
                // Save File to database
                saveToDb: ['metadata',function(cb,r){
                    File.create({
                        name: file.name,
                        size: file.size,
                        fsName: r.metadata.newFileName
                    }).success(function(f) {
                        // Move file into target directory, inheriting permissions
                        f.mv(parsedFormData.parent.id,cb);
                    });
                }],
                // Write file to destination
                writeFile: ['readFile','metadata',function(cb,r){
                        console.log("writing to",r.metadata.newPath);
                        console.log("file data",r.readFile,r.metadata);
                    fs.writeFile(r.metadata.newPath,r.readFile,cb);
                }]
            },function(err,res){
                resultSet.push({
                    id: res.saveToDb.id,
                    name: res.saveToDb.name,
                    size: res.saveToDb.size,
                    DirectoryId: parsedFormData.parent.id
                })              
                cb(err,res);
            });
        },
        // And respond
        function(err) {

            // Respond to client with success or failure
            if (err) { res.json({success:false, error: err}); }
            else { res.json({   success: true}); }
        });

    },
...
}

from sails.

dzcpy avatar dzcpy commented on May 24, 2024

Thanks for sharing this. I'm not sure if this the correct place to ask. But for uploaded files, where should we store them? .tmp/ or assets/? Other folders seem not visible from the client side. But if I store files in assets folder, it will only be available after restarting sails right? If store in .tmp folder, since it's got a 'tmp' in its name, will it be deleted later by chance?

from sails.

sgress454 avatar sgress454 commented on May 24, 2024

As long as Grunt is running, which it should be as long as Sails is lifted, anything saved under /assets should automatically be copied to the appropriate place in .tmp/public. This is a fine strategy as long as you're okay with everyone having full access to everyone else's uploaded files! Otherwise, best to save them in a non client-facing dir, and set up a controller action to stream them down. Then you can use policies for access control.

from sails.

dzcpy avatar dzcpy commented on May 24, 2024

Thanks a lot for the help, I think I'll try the latter one, don't want all resource files to be duplicated in .tmp folder, it takes double space.

from sails.

danielfttorres avatar danielfttorres commented on May 24, 2024

Does anyone have a example running a file upload?

from sails.

tkh44 avatar tkh44 commented on May 24, 2024

@danielfeelfine Here is a simple example I ripped out of a test project of mine.
https://gist.github.com/tkh44/8225384

from sails.

danielfttorres avatar danielfttorres commented on May 24, 2024

Thx @tkh44, I go testing.

from sails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.