详解Vue+axios+Node+express实现文件上传(用户头像上传)
发布时间:2020-12-16 08:17:07 所属栏目:百科 来源:网络整理
导读:Vue 页面的代码 修改头像 axios接口 axios({ url: '/user/changeavatar',method: 'post',anync: true,contentType: false,processData: false,data: img}) js部分调用封装的接口 3145728) { alert('请选择3M以内的图片!'); return false; } let Form = new
Vue 页面的代码
修改头像
axios接口 axios({
url: '/user/changeavatar',method: 'post',anync: true,contentType: false,processData: false,data: img
})
js部分调用封装的接口 3145728) {
alert('请选择3M以内的图片!');
return false;
}
let Form = new FormData();
Form.append('avatar',img,this.avatar_name);
API.ChangeAvatar(Form)
.then((response) => {
console.log(response)
})
.catch((error) => {
console.log(error)
})
}
}
后台node代码app.post('/user/changeavatar',function(req,res) {
console.log(req.files); // the uploaded file object let avatar = req.files.avatar; // Use the mv() method to place the file somewhere on your server res.send('File uploaded!'); 在这里我并没有用multer,而是用别人的npm包express-fileupload 代码运行,成功将图片上传到了指定目录 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |