A place to hold mainly reading notes, and some technical stuff occasionally. 这里主要是一些读书笔记、感悟;还有部分技术相关的内容。
目录[-]
接触Node.js也有十来天了,算是学点新东西。The best way to learn something new is to use it.
学习新知识最好的方式莫过于去使用它(do something),在自然语言、编程语言的习得过程中尤为如此。
关键是如何通过socket.io实现前后端的实时通信:在routes中使用socket触发文件上传进度事件。
form.hash='md5'
计算文件唯一标识,保证上传后的文件不重复;var express = require('express');
var path = require('path');
var app = express();
var port = 8888;
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// include static files
app.use(express.static(path.join(__dirname, 'public')));
// upload destination setup
app.set('files', path.join(__dirname, '/public/files'));
var index = require('./routes/index');
// router
app.get('/upload', index.list);
app.post('/upload', index.submit(app.get('files')));
app.get('/file/:id/download', index.download(app.get('files')));
var server = app.listen(port);
console.log("Listening on port: " + port);
var io = require('socket.io')(server);
app.set('socketio', io); // store a reference to the io object, can be passed to routes
var formidable = require('formidable');
var fs = require('fs');
var path = require('path');
var fileUploaded = new Map(); // store uploaded files
exports.list = function (req, res) {
res.render('index', {
title: "File upload",
fileUploaded: fileUploaded
});
};
exports.submit = function (dir) {
return function (req, res, next) {
var form = new formidable.IncomingForm();
// high level formidable API
form.uploadDir = dir; // set destination
form.hash = 'md5'; // use hash algorithm, we can get hash value by 'file.hash'
form.parse(req, function (err, fields, files) {
// console.log(fields);
// console.log(files);
files.file.lastModifiedDate = files.file.lastModifiedDate.toLocaleString();
var f = {
newName: fields.name.length == 0
? files.file.name
: fields.name + path.extname(files.file.name),
file: files.file
};
if (fileUploaded.has(files.file.hash)) {
form.emit('aborted'); // doesn't work!?
console.log('aborted');
} else {
fileUploaded.set(files.file.hash, f); // add to map
}
fs.rename(files.file.path, path.join(form.uploadDir, files.file.name), function (err) {
if (err) {
console.log(err);
}
res.redirect('/upload');
console.log('Finished.');
});
});
var io = req.app.get('socketio'); // get reference to socket.io
// listening progress event and send data to client
form.on('progress', function (bytesReceived, bytesExpected) {
var percent = Math.floor(bytesReceived / bytesExpected * 100);
console.log(percent);
var progress = {
name: 'progress',
percent: percent
};
// emit event : progress
io.emit('progress', progress); //notify all client, no session here
});
}
};
exports.download = function (dir) {
return function (req, res, next) {
var id = req.params.id;
var file = fileUploaded.get(id);
var targetPath = path.join(dir, file.file.name);
// second parameter can be used to specify file name
res.download(targetPath, file.newName);
};
};
<table>
<caption>Uploaded Files</caption>
<thead>
<tr>
<th>Name</th>
<th>Size</th>
<th>Type</th>
<th>DateTime</th>
<!--<th>Details</th>-->
</tr>
</thead>
<tbody>
<% fileUploaded.forEach(function(item, key, mapObj) { %>
<tr>
<td><a href='/file/<%=key%>/download'><%=item.newName%></a></td>
<td><%=item.file.size%></td>
<td><%=item.file.type%></td>
<td><%=item.file.lastModifiedDate%></td>
<!--lastModifiedDate, mtime???-->
<!--<td><%=JSON.stringify(item.file)%></td>-->
</tr>
<%})%>
</tbody>
</table>
// client validation
function validateInput() {
var uploadButton = document.forms[0].upload;
uploadButton.onclick = function () {
var file = document.forms[0].file.value;
if (file.length == 0) {
document.getElementById('uploadProgress').innerText = 'No file selectd!';
return false;
}
var name = document.forms[0].name.value;
if (name.length != 0) {
var invalidChars = name.match(/[^a-zA-Z0-9_()\u4e00-\u9fa5]+/g); // match invalid file name
if (invalidChars.length != 0) {
document.getElementById('uploadProgress').innerText = 'Invalid file name!';
return false;
}
}
}
}
// communication between client and server using socket.io
function getUploadProgressFromServer() {
var socket = io.connect("http://localhost:8888");
var uploadProgress = document.getElementById("uploadProgress");
var bar = document.getElementById('bar');
socket.on("progress", function (data) {
if ("progress" === data.name) {
uploadProgress.innerText = data.percent + '%';
bar.style.width = data.percent / 100 * document.body.offsetWidth + 'px';
} else {
console.log("There is nothing.", data);
}
});
}
{"size":102056,
"path":"D:\\...\\uploadProgress\\public\\files\\upload_0dcfbb98abc3b2c7356f87f218df715b",
"name":"debug.log",
"type":"application/octet-stream",
"mtime":"2017-04-26 20:58:08",
"hash":"40f2f78bff67c9e6164aa790f9627d83"}
file.mtime
获取该属性时,竟然是undefined
,但是可通过file.lastModifiedDate
来获取此属性,也是醉了,这到底是什么原因??上述问题,如您知道原因,还请不吝赐教,非常感谢!
If you have any questions or any bugs are found, please feel free to contact me.
Your comments and suggestions are welcome!