<el-button
class="file"
v-loading.fullscreen.lock="fullscreenLoading"
element-loading-text="上传中"
>
<input type="file" @change="change" />
点击上传
</el-button>
获取文件流
const change = (e: any) => {
console.log(e.target.files[0]);
// 手写的input需要一个对象将本地图片转换为对应的格式来上传
let formData = new FormData();
// e.target.files就是选中的文件的一个数组
formData.append("file", e.target.files[0]);
formData.append("type", "1");
fullscreenLoading.value = true;
api.post("/api/file/uploadImages", formData).then((res: any) => {
console.log(res);
form.value.detailsImgAndPageUrl = res.body;
fullscreenLoading.value = false;
});
};
更改原生上传样式
.file {
position: relative;
width: 100px;
background-color: #ccc;
text-align: center;
}
.file input {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
opacity: 0;
}
.file:hover {
background: #aadffd;
border-color: #78c3f3;
color: #004974;
text-decoration: none;
}
该文章展示了如何在Vue.js应用中创建一个自定义样式的文件上传按钮,利用ElementUI的el-button组件结合v-loading指令实现上传状态显示。当用户选择文件后,使用FormData对象处理文件数据并附带额外参数,然后通过axios的post方法向服务器发送请求进行文件上传。同时,文章还介绍了如何修改原生input[type=file]的样式。
530






