function upload(response, request) {
console.log(“Request handler ‘upload’ was called.”);
var form = new formidable.IncomingForm();
// parse a file upload
console.log(“about to parse”);
form.parse(request, function(error, fields, files) {
console.log(“parsing done”);
fs.renameSync(files.upload.path, “/tmp/test.jpg”);
response.writeHead(200, {
“Content-Type”: “text/html”
});
response.write("received image:
“);
response.write(”");
response.end();
});
}
node-formidable会处理将上传的文件保存到本地/tmp目录中,确保该文件保存成/tmp/test.jpg 这里采用fs.renameSync(path1,path2)来实现 但是在运行时报错了,请问啥原因呢
var form = new formidable.IncomingForm();
var uploadDir = "you upload dir";
form.uploadDir = uploadDir;
form.on('fileBegin', function (name, file) {
file.path = path.resolve(uploadDir, "the file name you want ");
}).on('field', function (name, value) {
console.log('get filed', name, value);
}).on('file', function (name, file) {
console.log('got file');
}).on('progress', function (bytes) {}).on('end', function () {
console.log('end');
}).on('error', function (err) {
console.log('error', err);
return res.jsonDBE('error ' + err);
}).parse(req);