nodejs -formidable模块实现图片上传。

时间:2024-12-04 19:02:50

var form = new formidable.IncomingForm();
form.uploadDir="/localnonobank/test/images/";   //存储路径
form.on('file', function(name,file){
filepath=file.path;
filename=file.name;
});
form.parse(request, function(err,fields,files){
if(err){
throw err;
};
});

form.on('end', function() {
fs.rename(filepath,form.uploadDir+filename);
response.end(JSON.stringify({name:filename,src:form.uploadDir+filename}));
});