我使用Formidable上传文件,但是出现了以下错误。
当我删除了
Error: EXDEV: cross-device link not permitted, rename
这是我的代码:
router.post('/file',function(req,res) {
var form = new formidable.IncomingForm();
function checkFile(filename) {
if(filename.match(/\.(jpg|jpeg|png)$/i)){
return form.uploadDir = path.join(__dirname, '../public/uploads/img');
}else{
return form.uploadDir = path.join(__dirname, '../public/uploads');
}
}
form.multiples = true;
form.maxFieldsSize = 2 * 300 * 300;
// every time a file has been uploaded successfully,
// rename it to it's orignal name
form.on('file', function(field, file) {
var fileName = file.name;
var d = new Date();
var t = d.getTime();
var newName = md5(file.name) + t;
fs.rename(file.path,path.join(checkFile(fileName),newName),function(err) {
if(err)
console.log(err);
console.log('Success')
});
});
// log any errors that occur
form.on('error', function(err) {
console.log('An error has occured: \n' + err);
});
// parse the incoming request containing the form data
form.parse(req, function(err, fields, files) {
});
})
当我删除了
function checkFile
并将其更改为form.uploadDir = path.join(__dirname, '../public/uploads');
时,它就能完美运行。我的错误在哪里?请帮助我。