vue上传图片
html
<div id="file">
<input class="inputtt" type="file" @change="upfile($event)" accept="image/*" />
</div>
js
//上传图片
upfile(event) {
var file = event.target.files[0]
var name = event.target.files[0].name;
lrz(file).then(rst => { //压缩图片
file = rst.file;
let file = new window.File([file], name, {
type: file.type
}) //把blob转化成file
let formData = new FormData();
formData.append(‘file‘, file);
formData.append(‘fileType‘, ‘IMAGE‘)
this.axios({
url: ‘API‘,
method: ‘post‘,
headers: {
‘content-type‘: ‘application/x-www-form-urlencoded‘,
"token": token //token换成从缓存获取
},
data: formData
}).then(res => {
if(res.data.code == 200) {
}
})
});
},
原文地址:https://www.cnblogs.com/cai001018/p/cc.html