Vant Uploader和axios结合上传图片到后台获取url
html代码:
<van-uploader :after-read="afterRead" :upload-text="'上传图片'" :before-read="beforeRead" />
上传js代码:
afterRead (file) {
this.uploadImg(file.file)
},
beforeRead (file) {
if (file.type !== 'image/jpeg' && file.type !== 'image/png') {
Toast('请上传 jpg/png 格式图片');
return false;
}
let isLt1M = file.size / 1024 / 1024 <= 1
if (!isLt1M) {
Toast('图片大小1M以内');
return false
}
return true;
},
uploadImg (file) {
let formdata1 = new FormData();
formdata1.append('file', file, file.name);
let config = {
headers:{
'Content-Type':'multipart/form-data'
}
};
const axiosAjax = this.axios.create({
timeout: 1000 * 60,
withCredentials: true
});
axiosAjax.post('url',formdata1,config).then((res)=>{
console.log(res.data);
}).catch(() => {})
}