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:<br/>”); response.write("<img src=’/show’ />"); response.end(); }); }
node-formidable会处理将上传的文件保存到本地/tmp目录中,确保该文件保存成/tmp/test.jpg 这里采用fs.renameSync(path1,path2)来实现 但是在运行时报错了,请问啥原因呢
no such file or directory
第一件事儿是格式化代码,不然别人看不懂的,多谢 @MiguelValentine
好的 谢谢
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);
没有E:\tmp 目录,新建一个tmp目录或者更改目录