服务器代码:
app.post('/student/submit',(req,res)=>{
const form=new formidable.IncomingForm();
form.uploadDir=path.join(__dirname,'studentFile/jk5192')
form.keepExtensions=true;
form.parse(req,(a,fields,files)=>{
if(a){
res.status(400);
}
var oldpath=files.a.path;
var newpath=__dirname +'/studentFile/jk5192'+ '/' +files.a.name ;
fs.rename(oldpath,newpath,function(err){
if(err){
console.log('改名失败')
}
res.end('ok');
})
})
})
前端请求代码:
$.ajax({
type:'post',
url:'/student/submit',
data:formdata,
contentType:false,
processData:false,
success:function(e){
alert('提交成功')
},
error:function(){
alert('提交失败')
}
})