用node.js写一个图片上传功能,我看网上的示例是这样的
var form = new formidable.IncomingForm(); //设置文件上传存放地址 form.uploadDir = "./uploads"; //执行里面的回调函数的时候,表单已经全部接收完毕了。 form.parse(req, function(err, fields, files) { var oldpath = __dirname + "/" + files.tupian.path; //新的路径 var newpath = __dirname + '/uploads/'+t+ran+extname; //改名 fs.rename(oldpath,newpath,function (err) { if(err){ throw Error("改名失败"); } res.writeHead(200, {'content-type': 'text/plain'}); res.end("成功"); });
这个例子里的 oldpath是什么取法? “files.tupian.path”,这是什么取法?tupian是这么冒出来的
var oldpath = __dirname + "/" + files.tupian.path;
我自己试了一下
var formidable = require("formidable"); var fs = require("fs"); var path = require("path"); var form = new formidable.IncomingForm(); //设置上传文件夹 form.uploadDir = path.normalize(__dirname + "/../public/avatar"); form.parse(req, function (err, fields, files) { console.log(files); var oldpath = __dirname + "/" + files.touxiang.path; });
files能打印出来,我要取的path也有,
但我该怎么取哪个path,如果像那个例子里那样取的话会报错“TypeError: Cannot read property 'path' of undefined”
各个包的版本如下
"dependencies": { "express": "^4.16.3", "express-session": "^1.15.6", "formidable": "^1.2.1", "fs": "^0.0.1-security", "md5": "^2.2.1", "mongodb": "^2.0.5", "mongoose": "^5.0.12", "path": "^0.12.7" }
pardon110
相关分类