Commit 2f640781 authored by ganjingcun's avatar ganjingcun

文件上传

parent ceed3e79
...@@ -8,3 +8,4 @@ test/e2e/reports ...@@ -8,3 +8,4 @@ test/e2e/reports
db.config.js db.config.js
test.js test.js
db.sql db.sql
upload/
...@@ -35,6 +35,7 @@ app.use(bodyParser.json()); ...@@ -35,6 +35,7 @@ app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser()); app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public'))); app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'upload')));
app.use('/', index); app.use('/', index);
app.use('/users', users); app.use('/users', users);
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
"debug": "~2.2.0", "debug": "~2.2.0",
"eventproxy": "^0.3.5", "eventproxy": "^0.3.5",
"express": "~4.14.0", "express": "~4.14.0",
"formidable": "^1.1.1",
"jade": "~1.11.0", "jade": "~1.11.0",
"moment": "^2.18.1", "moment": "^2.18.1",
"morgan": "~1.7.0", "morgan": "~1.7.0",
......
...@@ -483,6 +483,13 @@ router.get('/deckinfo', function (req, res) { ...@@ -483,6 +483,13 @@ router.get('/deckinfo', function (req, res) {
}); });
}); });
var file = require("./file.js");
router.post('/upload', file.upload);
router.get('/download/:id', file.download);
router.post('/deckinfo', function (req, res) { router.post('/deckinfo', function (req, res) {
let author = req.body.user; let author = req.body.user;
...@@ -641,6 +648,7 @@ router.get('/user', function (req, res) { ...@@ -641,6 +648,7 @@ router.get('/user', function (req, res) {
// to run a query we can acquire a client from the pool, // to run a query we can acquire a client from the pool,
// run a query on the client, and then return the client to the pool // run a query on the client, and then return the client to the pool
pool.connect(function (err, client, done) { pool.connect(function (err, client, done) {
if (err) { if (err) {
return console.error('error fetching client from pool', err); return console.error('error fetching client from pool', err);
} }
...@@ -666,6 +674,7 @@ router.get('/user', function (req, res) { ...@@ -666,6 +674,7 @@ router.get('/user', function (req, res) {
if (!username) { if (!username) {
// return res.status(404).send('username can not be null') // return res.status(404).send('username can not be null')
done();
return res.json(resultData) return res.json(resultData)
} }
......
var formidable = require('formidable');
var fs = require('fs'); //node.js核心的文件处理模块
exports.upload = function (req, res, next) {
var message = '';
var form = new formidable.IncomingForm();
form.encoding = 'utf-8';
form.uploadDir = 'upload/';
form.keepExtensions = true;
form.maxFieldsSize = 2 * 1024 * 1024;
form.parse(req, function (err, fields, files) {
if (err) {
console.log(err);
return res.status(500).send('upload image fail!')
}
console.log(files)
console.log(files.file.path)
var response = {};
if (err) {
response.code = 500;
} else {
response.code = 200;
response.path = files.file.path;
}
res.json(response);
});
};
exports.download = function (req, res) {
var filename = req.params.id
var filepath = 'upload/' + filename
// filename:设置下载时文件的文件名,可不填,则为原名称
res.download(filepath, filename);
};
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment