nodejs fs文件系统

var express = require("express");
var main = express();
var fs = require('fs');//引入文件读取模块
var multiparty = require('multiparty');
var path = require('path');
//监听8000端口
//设定静态地址路径
main.use(express.static(__dirname+"/static"));
//指定参数body来处理
main.use(express.methodOverride());
//main.use(express.bodyParser());
main.all("/",function(req,res){
res.sendfile("./static/fileTest.html")
});
var localFile="E:/v1/ezfix1";本地文件路径
main.post("/upload",function(req,res){
var form = new multiparty.Form({uploadDir: localFile});
//console.log(form);
form.encoding = 'utf-8'; //设置编辑
form.parse(req, function(err, fields, files) {
var filesTmp = JSON.stringify(files, null, 2);
if (err) {
console.log('parse error: ' + err);
} else {
var inputFile = files.codecsv[0];
var uploadedPath = inputFile.path;
console.log(uploadedPath);
var dstPath = inputFile.originalFilename;
var img=localFile+'/'+dstPath;
fs.exists(img, function(exists){
if(exists){
fs.unlink(uploadedPath,function(err){
if(err){
console.log(err);
}
});
res.send(JSON.stringify({"success":false,"msg":"图片存在!"}));
}else{
//重命名为真实文件名
fs.rename(uploadedPath, localFile+"/"+dstPath, function(err) {
if (err) {
res.send(JSON.stringify({"success":false,"msg":"上传失败"}));
} else {
res.send(JSON.stringify({"success":true,"imgName":dstPath,"msg":"上传成功"}));
}
});
}
});
}
});
});

main.post("/loading",function(req,res){
console.log(req.body);
var fileName=req.body.name;
console.log(fileName);
console.log(localFile+'/'+fileName+'.png');
fs.readFile( localFile+'/'+fileName+'.png' , function(err,data){
/*
一参为文件路径
二参为回调函数
回调函数的一参为读取错误返回的信息,返回空就没有错误
二参为读取成功返回的文本内容
*/
if(err){
res.writeHeader(404,{
'content-type' : 'text/html;charset="utf-8"'
});
res.write('<h1>404错误</h1><p>你要找的页面不存在</p>');
res.end();
}else{
res.writeHeader(200,{
'content-type' : 'text/html;charset="utf-8"'
});
res.write(data);//将index.html显示在客户端
res.end();
}

});
});

//main.post('/:service?', function(req, res){
// if (req.files && req.files.codecsv != 'undifined') {
// var temp_path = req.files.codecsv.path;
// if (temp_path) {
// fs.readFile(temp_path, 'utf-8', function(err, content) {
// //文件的内容
// console.log('content',content);
// // 删除临时文件
// fs.unlink(temp_path);
// });
// }
// }
//
//});


main.post("/delFile",function(req,res){
var form = new multiparty.Form({uploadDir: localFile});
form.encoding = 'utf-8'; //设置编辑
form.keepExtensions = true; //保留后缀
form.maxFieldsSize = 200 * 200; //文件大小
form.parse(req, function(err, fields, files) {
var fileName=fields.name[0];
var img=localFile+'/'+fileName;
if(fileName){
var inputFile = files.codecsv[0];
var uploadedPath = inputFile.path;
var dstPath = inputFile.originalFilename;
//重命名为真实文件名
fs.rename(uploadedPath, localFile+"/"+dstPath, function(err) {
if (err) {
res.send(JSON.stringify({"success":false,"msg":"上传失败"}));
} else {
res.send(JSON.stringify({"success":true,"imgName":dstPath,"msg":"上传成功"}));
}
});
}else{
// 判断文件img是否存在
fs.exists(img, function(exists){
console.log(exists);
if(exists){
fs.unlink(img,function(err){
if(err){
res.send(JSON.stringify({"success":false,"msg":"删除不成功"}));
}else{
//res.send(JSON.stringify({"success":true,"msg":"删除成功"}));
var inputFile = files.codecsv[0];
var uploadedPath = inputFile.path;
var dstPath = inputFile.originalFilename;
//重命名为真实文件名
fs.rename(uploadedPath, localFile+"/"+dstPath, function(err) {
if (err) {
res.send(JSON.stringify({"success":false,"msg":"上传失败"}));
} else {
res.send(JSON.stringify({"success":true,"imgName":dstPath,"msg":"上传成功"}));
}
});
}
})
}else{
res.send(JSON.stringify({"success":true,"msg":"文件不存在!"}));
}
});
}
});
});


//添加 修改 上传图片到本地文件夹
main.post("/uploadImg",function(req,res){
var form = new multiparty.Form({uploadDir: localFile});
form.encoding = 'utf-8'; //设置编辑
form.keepExtensions = true; //保留后缀
form.maxFieldsSize = 200 * 200; //文件大小
form.parse(req, function(err, fields, files) {
var updateImgName="";
var img="";
if(fields.name){
updateImgName=fields.name[0];
img=localFile+'/'+updateImgName;
}
var inputFile = files.codecsv[0];
var uploadedPath = inputFile.path;
var dstPath = inputFile.originalFilename;
if(!updateImgName){//上传功能
//重命名为真实文件名
fs.rename(uploadedPath, localFile+"/"+dstPath, function(err) {
if (err) {
res.send(JSON.stringify({"success":false,"msg":"上传失败"}));
} else {
res.send(JSON.stringify({"success":true,"imgName":dstPath,"msg":"上传成功"}));
}
});
}else{//修改图片---删除
// 判断修改之前的文件是否存在
fs.exists(img, function(exists){
console.log(exists);
if(exists){//存在删除
fs.unlink(img,function(err){
if(err){
console.log(err);
}
})
}
//重命名为真实文件名
fs.rename(uploadedPath, localFile+"/"+dstPath, function(err) {
if (err) {
res.send(JSON.stringify({"success":false,"msg":"上传失败"}));
} else {
res.send(JSON.stringify({"success":true,"imgName":dstPath,"msg":"上传成功"}));
}
});
});
}
});
});



main.listen("9000",function(){
console.log("监听9000端口");
});
posted @ 2016-10-14 16:40  小贱贱!  阅读(125)  评论(0)    收藏  举报