Comments (8)
https://github.com/felixge/node-formidable
from sails.
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.
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.
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.
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.
Does anyone have a example running a file upload?
from sails.
@danielfeelfine Here is a simple example I ripped out of a test project of mine.
https://gist.github.com/tkh44/8225384
from sails.
Thx @tkh44, I go testing.
from sails.
Related Issues (20)
- i am not able to access sails even after ( "sails" : true ) set in config/global.js file "ReferenceError: sails is not define" HOT 2
- Remove socket error logs during automatic disconnect/reconnect of Redis/Sails app HOT 1
- Commit https://github.com/balderdashy/sails/commit/4a023dc5095a4b30fdc8535f705ed34cd22d2f7d breaks websocket sessions HOT 6
- Default values for options mentioned in config/global.js file? HOT 4
- support for ICU Format in i18n HOT 2
- BUG mutation of object valuesToSet in update blueprints action HOT 2
- _method with value put not working on form submit HOT 1
- Duplicate singleton instances after upgrading from 0.12.14 to 1.5.8 HOT 1
- Not working with mongodb server less. HOT 4
- Session issue with latest passport js HOT 1
- Getting Vite working with Sails.js/Parasails HOT 5
- Update and improve the documentation on Testing HOT 4
- Opentelemetry instrumentation support for sails HOT 7
- Using the latest socket.io version HOT 3
- Waterline has very bad compatibility with PostgreSQL it's almost unusable HOT 3
- Connection unstable HOT 2
- Datastore selection for Archive model HOT 1
- getDatastore.manager issue in [email protected] HOT 9
- [email protected] captains-log dependency introduces ReDoS Vulnerability HOT 5
- Testing with Parasails HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sails.