Closed ghost closed 10 years ago
I used the mv
node package to help me with this.
function(req, res) {
var file = req.files.file;
var tempPath = file.path;
var path = appDir + '/public' + '/res' + '/' + file.name;
console.log(path);
mv(tempPath, path, function(err) {
console.log(err);
if (err){
res.send(500);
}else{
res.send(200);
}
});
}
Oddly this question came up just one day ago!
Please see here if you're still looking for some answers, or have additional feedback: https://github.com/nervgh/angular-file-upload/issues/88
The example use php, but I'm not familiar with that framework. How would I go about saving the uploaded files in a Node server?
I'll add an example with Node.js in the near future.
Any chance of that node example? :)
@nervgh when will the example with Node.js been ok?
u just need multiparty in the server side
add https://github.com/emps/node-multiparty than var multiparty = require('multiparty');
...
var form = new multiparty.Form({
uploadDir: __dirname + '/uploads' /* make sure u have created that folder */
});
...
form.parse(req, function(err, fields, files) { /* when its uploaded u can do rest of what u need from here.. */ function is the callback so upload completes it get run
I use express on the back end, and ended up using connect-busboy middleware which I found very easy to use. It augments requests with a busboy
member.
Configure express to use busboy:
var busboy = require('connect-busboy');
...
app.use(busboy());
Create an upload
module:
'use strict';
var fs = require('fs');
exports.file = function(req, res) {
req.pipe(req.busboy);
req.busboy.on('file', function(fieldname, file, filename) {
var fstream = fs.createWriteStream('./images/' + filename);
file.pipe(fstream);
fstream.on('close', function () {
res.redirect('back');
});
});
};
Then mount the upload module at the appropriate endpoint (express v4.0 shown):
var upload = require('./controllers/upload');
...
app.route('/api/upload')
.post(upload.file);
Any chance of that node example? :)
Sorry guys. I remember about it but I'm very busy now.
Hey, sorry if I'm a slightly late to the party, but I figure someone might find this of use. I went with Multer as my middleware of choice. I'm working with: Angular 1.5.6, angular-file-upload 2.3.3, Express 4.13.1, and Multer 1.1.0.
In your Angular controller:
// controller.js
...
uploader = $scope.uploader = new FileUploader({
url: '/upload' // POST request will be handled by Multer
})
...
This will make sure that when a file is uploaded from the queue on the Angular side, it gets sent to the backend to be dealt with appropriately.
I decided to put the upload logic in a seperate module, so you'll want to load the router module in the app:
// app.js
var upload = require('./routes/upload')
...
app.route('/upload', upload)
Now, here is where the upload is taken care of:
// upload.js
var express = require('express')
var router = express.Router()
var multer = require('multer')
// Storage option can be changed - check Multer docs
var storage = multer.diskStorage({
destination: function(req, file, cb) {
var path = './uploads' // Make sure this path exists
cb(null, path)
}})
var upload = multer({
storage: storage
})
// Will handle POST requests to /upload
router.post('/', upload.single('file'), function(req, res) {
res.status(204).end()
})
module.exports = router
I've decided to go with disk storage, but Multer gives you a couple different storage choices. The upload.single('file')
can be swapped out depending on your needs, and you can also set some limits and filters for the upload files.
And there you have it, a Node + Express back-end. Hope that helps!
The example use php, but I'm not familiar with that framework. How would I go about saving the uploaded files in a Node server?