【基于node.js+express的web开发备忘】--技术分享(TShare)站点-资源模块

资源模块:可以查找整个系统中的所有上传资源的信息、可以支持用户上传资源的查看以及上传、可以支持查看已下载资源的功能。

【基于node.js+express的web开发备忘】--技术分享(TShare)站点-资源模块_第1张图片

/*跳转至资源界面*/
router.get('/resource', function(req, res) {
	if(!req.session.user) {
		return res.redirect('/');
	}
	var msg = null;
	Resource.query({}, function (err, docs) {
		if(err) {
			console.log(err);
		}
		res.render('index', { title: '资源', 
												toUrl: '/resource',
												toBodyUrl: '/all',
												user: req.session.user,
												items: docs,
												msg: msg});
	});
});
/*下载资源*/
router.post('/doDownLoad', function(req, res) {
	if(!req.session.user) {
		return res.redirect('/');
	}
	//console.log("req.body=="+JSON.stringify(req.body));
	//console.log("req.session.user=="+JSON.stringify(req.session.user))
	var filePath = Util.trim(req.body.filePath);
	
	var downLoadResource = new DownResource({
			//下载者
			downLoadName: req.session.user.userName,
			//下载者昵称
			downLoadNickName: req.session.user.nickName,
			//下载时间
			downloadTime: Util.getToday('yyyy-MM-dd HH:mm:ss'),
			//资源名称
			resName: req.body.fileName,
			//资源描述
			resDesc: req.body.fileDesc,
			//资源路径
			resPath: filePath,
			//资源格式
			resStyle: req.body.fileStyle,
			//上传时间
			uploadTime: req.body.uploadTime,
			//上传者
			userName: req.body.userName,
			//上传者昵称
			nickName: req.body.nickName
	});
	//console.log("downLoadResource=="+JSON.stringify(downLoadResource));
	downLoadResource.save(function(err) {
		if(err) {
			console.log(err);
		}
	});
	
	if(filePath != '') {
		var fileName = encodeURI(filePath.substr(filePath.lastIndexOf("/")+1));
		fs.readFile("."+filePath, "binary", function(err, file){
			if(err){
				res.write(err+"\n");
				res.end();
			}else{
				res.writeHead(200, {"Content-Type": "application/octet-stream;charset=UTF-8", "Content-Disposition": "attachment; filename="+fileName}); 
				res.write(file, "binary");
				res.end();
			}
		});
	} else {
		res.redirect('/resource'); 
	}
});



【基于node.js+express的web开发备忘】--技术分享(TShare)站点-资源模块_第2张图片

/*跳转至资源界面*/
router.get('/resource/upload', function(req, res) {
	if(!req.session.user) {
		return res.redirect('/');
	}
	var msg = null;
	if(req.param('res_msg') != undefined) {
		msg = req.param('res_msg');
	}
	Resource.query({userName:req.session.user.userName}, function (err, docs) {
		if(err) {
			console.log(err);
		}
		
		res.render('index', { title: '资源', 
												toUrl: '/resource',
												toBodyUrl: '/upload',
												user: req.session.user,
												items: docs,
												msg: msg});
	});
});


 

/*上传资源*/
router.post('/doUpload', function(req, res) {
	if(!req.session.user) {
		return res.redirect('/');
	}
	var resObj = req.files.uploadResource;
	var resName = resObj.name;
	if(resName == '') {
		//未添加资源!
		return res.redirect('/resource/upload?res_msg=0'); 
	}
	/*
		//文件大小1M
		if(fileObj.size > 1048576) {
			//上传图片太大!
			return res.redirect('/resource/upload?res_msg=1'); 
		}
		*/
	var resStyle = resName.substr(resName.indexOf(".")+1);
	var resourceName = Util.trim(req.body.resourceName);
	if(resourceName == '') {
		resourceName = resName.substr(0, resName.indexOf("."));
	}
	var resourceDesc = Util.trim(req.body.resourceDesc);
	if(resourceDesc == '') {
		resourceDesc = '--';
	}
	
	//-------------------------文件上传处理-------------------------------------
	fs.rename(resObj.path, "./upload/"+resourceName+"."+resStyle, function(err) {
		if(err){
			console.log(err);
			//上传过程中出错!
			return res.redirect('/resource/upload?res_msg=2');
		}
		
		var resource = new Resource({
				//资源唯一编码
				qno: Util.sequenceCD(),
				//资源名称
				resName: resourceName,
				//资源描述
				resDesc: resourceDesc,
				//资源路径
				resPath: '/upload/'+resourceName+"."+resStyle,
				//资源格式
				resStyle: resStyle,
				//上传时间
				uploadTime: Util.getToday('yyyy-MM-dd HH:mm:ss'),
				//上传者
				userName: req.session.user.userName,
				//上传者昵称
				nickName: req.session.user.nickName
		});
		//上传文件信息
		resource.save(resource, function(err, user) {
			if(err) {
				console.log(err);
				//上传文件信息保存过程中出错!
				return res.redirect('/resource/upload?res_msg=error'); 
			}
		});
		res.redirect('/resource/upload'); 
	});
	//--------------------------------------------------------------
});

/*跳转至资源界面*/
router.get('/resource/download', function(req, res) {
	if(!req.session.user) {
		return res.redirect('/');
	}
	var msg = null;
	if(req.param('res_msg') != undefined) {
		msg = req.param('res_msg');
	}
	DownResource.query({downLoadName:req.session.user.userName}, function (err, docs) {
		if(err) {
			console.log(err);
		}
		
		res.render('index', { title: '资源', 
												toUrl: '/resource',
												toBodyUrl: '/download',
												user: req.session.user,
												items: docs,
												msg: msg});
	});
});


以上仅为备忘或者分享,若有问题请大家指正或讨论!!


 

你可能感兴趣的:(web开发,express,nodejs博客案例)