forked from valor-software/ng2-file-upload
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated: new backend demo & fix ng2-bs version
- Loading branch information
Showing
3 changed files
with
36 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,49 @@ | ||
var express = require('express'); | ||
var Busboy = require('busboy'); | ||
var path = require('path'); | ||
var multer = require('multer'); | ||
var fs = require('fs'); | ||
|
||
var app = express(); | ||
|
||
var DIR = './uploads/'; | ||
|
||
var upload = multer({dest: DIR}); | ||
|
||
app.use(function (req, res, next) { | ||
res.setHeader('Access-Control-Allow-Origin', '!put your host here!'); | ||
res.setHeader('Access-Control-Allow-Origin', 'http://valor-software.github.io'); | ||
res.setHeader('Access-Control-Allow-Methods', 'POST'); | ||
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); | ||
res.setHeader('Access-Control-Allow-Credentials', true); | ||
next(); | ||
}); | ||
|
||
app.use(multer({ | ||
dest: DIR, | ||
rename: function (fieldname, filename) { | ||
return filename + Date.now(); | ||
}, | ||
onFileUploadStart: function (file) { | ||
console.log(file.originalname + ' is starting ...'); | ||
}, | ||
onFileUploadComplete: function (file) { | ||
console.log(file.fieldname + ' uploaded to ' + file.path); | ||
} | ||
})); | ||
|
||
app.get('/api', function (req, res) { | ||
res.end('file catcher example'); | ||
}); | ||
|
||
app.post('/api', function (req, res) { | ||
var fstream; | ||
var files = []; | ||
var busboy = new Busboy({headers: req.headers}); | ||
busboy.on('file', function (fieldname, file, filename) { | ||
fstream = fs.createWriteStream(__dirname + '/uploads/' + filename); | ||
file.pipe(fstream); | ||
fstream.on('close', function () { | ||
files.push(filename); | ||
file.resume(); | ||
}); | ||
}); | ||
upload(req, res, function (err) { | ||
if (err) { | ||
return res.end(err.toString()); | ||
} | ||
|
||
busboy.on('finish', function () { | ||
res.end('ok'); | ||
res.end('File is uploaded'); | ||
}); | ||
req.pipe(busboy); | ||
}); | ||
|
||
app.listen(process.env.PORT || 3000); | ||
var PORT = process.env.PORT || 3000; | ||
|
||
app.listen(PORT, function () { | ||
console.log('Working on port ' + PORT); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters