Skip to content

Commit

Permalink
Add UpLoad
Browse files Browse the repository at this point in the history
  • Loading branch information
WNLee committed Oct 5, 2013
1 parent cc04e6c commit caeb021
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ app.set('view engine', 'ejs');
app.use(flash());
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.bodyParser({ keepExtensions: true, uploadDir: './public/file'}));
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({
Expand Down
Binary file added public/file/lufei.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/

var crypto = require('crypto'),
fs = require('fs'),
User = require('../models/user.js'),
Post = require('../models/post.js');

Expand All @@ -22,6 +23,34 @@ module.exports = function(app) {
});
});
});
app.get('/upload', checkLogin);
app.get('/upload', function (req, res) {
res.render('upload', {
title: '文件上传',
user: req.session.user,
success: req.flash('success').toString(),
error: req.flash('error').toString()
});
});
app.post('/upload', checkLogin);
app.post('/upload', function (req, res) {
for (var i in req.files) {
if (req.files[i].size == 0) {
// 若文件为空则删出此文件
fs.unlinkSync(req.files[i].path);
console.log('Successfully removed an empty file!');
} else {
var target_path = './public/file/' + req.files[i].name;
// 若文件不为空则重命名文件
console.log(target_path);
fs.renameSync(req.files[i].path, target_path);
console.log(req.files[i].path);
console.log('Successfully renamed a file!');
}
}
req.flash('success', '文件上传成功!');
res.redirect('/upload');
});
app.get('/reg', checkNotLogin);
app.get('/reg', function (req, res) {
res.render('reg', {
Expand Down
1 change: 1 addition & 0 deletions views/header.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<nav>
<span><a title="主页" href="/">home</a></span>
<% if (user) { %>
<span><a title="上传" href="/upload">upload</a></span>
<span><a title="发表" href="/post">post</a></span>
<span><a title="登出" href="/logout">logout</a></span>
<% } else { %>
Expand Down
10 changes: 10 additions & 0 deletions views/upload.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%- include header %>
<form method = "post" action='/upload' enctype='multipart/form-data' >
<input type="file" name="file1" /><br/>
<input type="file" name="file2" /><br/>
<input type="file" name="file3" /><br/>
<input type="file" name="file4" /><br/>
<input type="file" name="file5" /><br/>
<input type="submit" /><br/>
</form>
<%- include footer %>

0 comments on commit caeb021

Please sign in to comment.