vue项目中
html
<input type="file" id="upload_file" accept="image/png,image/jpeg,image/jpg" @change="fileChange" style="display: none" :multiple="multiple" >
:multiple="multiple" // 调起相机和相册
methods
//formData 上传服务器的参数
//uploadUrl 上传服务器的地址url
fileChange(e) {
let file = e.target.files[0];
let fileSize = file.size / 1024 / 1024 < 2;
if(fileSize) {
let formData = new FormData();
formData.append("fileName",file);
formData.append("json", JSON.stringify({"cid":"222"}));
axios.post(this.uploadUrl,formData,{headers: {'Content-Type':'multipart/form-data'}}).then((response) => {
this.imgUrl = response.data.data.url;
}).catch((error) => {
this.$toast.fail(error);
})
}else{
this.$toast("图片大小不得大于2M");
}
},