//图片上传路由 app.post('/uploadImg’,function(req, res, next){ console.log(req.files); console.log("name="+req.body.localUrl); console.log("文件的临时路径:"+req.files.imgFile.path);//这是文件的临时路径 console.log('get?dir=’+req.query.dir);
var tpm_path = req.files.imgFile.path;
var form = new formidable.IncomingForm();
form.keepExtensions = true;//保留后缀 .jpg/.png
var folderName = new Date().Format('yyyy-MM-dd');
var dirPath = "./public/uploadimg/"+folderName;//指定路径
console.log(dirPath);
if(!fs.existsSync(dirPath)){//如果没有这个文件夹就创建文件夹
fs.mkdirSync(dirPath);
}
//移动文件到指定路径
fs.rename(tpm_path, dirPath, function(err){
if(err){
throw err;
}
//删除临时文件
fs.unlink(tpm_path, function(err){
if(err){
throw err;
}
})
})
{ imgFile: { fieldName: 'imgFile’, originalFilename: 'pic.jpg’, path: 'public\uploadimg\1128-gapt0h.jpg’, headers: { 'content-disposition’: 'form-data; name="imgFile"; filename="pic.jpg"’, 'content-type’: ‘image/jpeg’ }, ws: { _writableState: [Object], writable: true, domain: null, _events: [Object], _maxListeners: 10, path: 'public\uploadimg\1128-gapt0h.jpg’, fd: null, flags: 'w’, mode: 438, start: undefined, pos: undefined, bytesWritten: 57771, closed: true, open: [Function], _write: [Function], destroy: [Function], close: [Function], destroySoon: [Function], pipe: [Function], write: [Function], end: [Function], setMaxListeners: [Function], emit: [Function], addListener: [Function], on: [Function], once: [Function], removeListener: [Function], removeAllListeners: [Function], listeners: [Function] }, size: 57771, name: 'pic.jpg’, type: ‘image/jpeg’ } } name=C:\fakepath\pic.jpg 文件的临时路径:public\uploadimg\1128-gapt0h.jpg get?dir=image ./public/2014-05-26/
G:\NodeJsWorkspace\blog\routes\index.js:240 throw err; ^ Error: EPERM, rename ‘G:\NodeJsWorkspace\blog\public\uploadimg\1128-gapt0h.jpg’