首页 > 其他分享 >vue3+vite+vant文件上传

vue3+vite+vant文件上传

时间:2023-03-15 13:58:54浏览次数:41  
标签:const vant resp value link file vue3 id vite

1:文件上传下载

 1 //上传文件
 2             const afterRead = () => {
 3                 for (let file of fileList.value) {
 4                     //1:加载状态
 5                     file.status = 'uploading';
 6                     file.message = '上传中...';
 7                     // 2.构建 FormData
 8                     const formData = new FormData();
 9                     formData.append("fileL", file.file);
10                     formData.append("file_name", file.file.name);
11                     formData.append("entity_code", "code");
12                     formData.append("entity_id", entity_id);
13                     // 3.发送
14                     save(formData).then(resp => {
15                         if (resp.success === true) {
16                             Toast('文件上传成功!')
17                             file.status = 'done';
18                             file.message = '上传完成';
19                             file.fileId = resp.id;
20                             getAll();
21                         } else {
22                             file.status = 'failed'
23                             file.message = '上传失败'
24                         }
25                     }).catch(() => {
26                         file.status = 'done ';
27                         file.message = '上传失败';
28                     });
29                 }
30             };
保存文件
1  <van-row justify="center">
2                 <van-col>
3                     <van-field label="文件上传" placeholder="请选择要上传的文件"/>
4                     <van-uploader v-model="fileList" accept="" :after-read="afterRead" multiple :max-count="4"
5                                   :max-size="500 * 1024" @oversize="onOversize" :before-delete="deleteFile"
6                                   result-type="file"
7                                   upload-text="选择文件"/>
8                 </van-col>
9             </van-row>
文件ui
 1  //删除确认
 2             const confirm = () => {
 3                 deleteLeaseFile({id: delId.value}).then(resp => {
 4                     if (resp.success) {
 5                         Toast('删除成功了');
 6                         getAll();//重新加载列表
 7                         show.value = false;
 8 
 9                     } else {
10                         Toast('删除失败!请重新操作');
11                     }
12                 }).catch(() => {
13                     Toast('删除失败!请重新操作');
14                 });
15 
16             }
删除文件
 1 //下载
 2 
 3             const down = (id, fileName) => {
 4                 downFile({id: id}).then(resp => {
 5 //下载方式一:
 6                     // console.log(resp);
 7                     // let url = window.URL.createObjectURL(new Blob([resp]));
 8                     // let link = document.createElement('a');
 9                     // link.style.display = 'none';
10                     // link.href = url;
11                     // link.setAttribute('download', fileName);
12                     // document.body.appendChild(link);
13                     // link.click();
14                     // document.body.removeChild(link); // 下载完成移除元素
15                     // window.URL.revokeObjectURL(url); // 释放掉blob对象
16 //下载方式二:支持移动端
17                     saveAs(new Blob([resp], {
18                         type: "application/octet-stream"
19                     }), fileName);
20 
21                 }).catch(() => {
22                 });
23             }
文件下载:2种方式

2:添加权限控制

 1 // 自定义指令
 2 export default {
 3     mounted(el, binding) {
 4         //当前用户所有权限
 5         const permissionBtn = localStorage.getItem('auths').split(',');
 6         // value 获取用户使用自定义指令绑定的内容
 7         const {value} = binding;
 8         if (value && value instanceof Array && value.length > 0) {
 9             const permissionFunc = value;
10             let num=0;
11            permissionFunc.forEach(a => {
12                 if (permissionBtn.indexOf(a)>=0) {
13                     num++;
14                 }
15             });
16             // 当用户没有这个按钮权限时,设置隐藏这个按钮
17             if (num===0) {
18                 el.style.display = 'none'
19             }
20         } else {
21             throw new Error('无权限');
22         }
23     }
24 }
25 
26 说明:
27 1:main.js导入
28 import permission from './api/permission.js'
29 2:使用 v-permission 直接添加到按钮上
30 <van-button v-permission="['AUTH_INFO_DE_V']" class="custom-button" color="linear-gradient(to right, rgb(255 185 78), #ffb037)"
31                                     type="primary" size="small" @click="onClick(dataItem['id'])">详细信息
32                         </van-button>
权限文件

 

标签:const,vant,resp,value,link,file,vue3,id,vite
From: https://www.cnblogs.com/angin-iit/p/17218216.html

相关文章

  • vue3仿windows弹窗
     一款基于vue3的仿windows弹窗。可以组件模板编写或函数式创建。地址:https://github.com/dnoyeb/box-win   安装npmadd'box-win'两种方式:1、组......
  • [Vue3] 组件上的ref不能与组件名相同
    情景关键组件没有正确引入函数无限递归解决如果在网上搜索[Vuewarn]:Componentismissingtemplateorrenderfunction.或[Vuewarn]:Invalidvnodetype......
  • 使用vscode + vite + vue3+ vant 搭建vue3脚手架
    【术栈】开发工具:VSCode代码管理:Git前端框架:Vue3构建工具:Vite路由:vue-router4x状态管理:vuex4xAJAX:axiosUI库:vant数据模拟:mockjscss预处理:sass构建vue3项目1,安装 vite......
  • less & saas/scss & css 深度选择器语法在 Vue2 & Vue3中的使用
    vue2中原生css>>>.el-card__header saas\scss::v-deep.el-card__headerless/deep/.el-card__header vue3中:deep(){......
  • webpack、vite、vue-cli、create-vue 的区别
    首先说结论Rollup更适合打包库,webpack更适合打包项目应用,vite基于rollup实现了热更新也适合打包项目。功能工具工具脚手架vue-clicreate-vue构建项目 vit......
  • vue vant rate评分组件无法点击取消归零功能处理
    //通过监听点击事件去判断是否触发change事件,如果没有则说明数据没变化<van-ratev-model="modelValue":disabled="!conf.isEditText"disabled-color="rgba(0,86,255,0.......
  • vue3 + vite 分析报告 report == rollup-plugin-visualizer
    安装插件 npmirollup-plugin-visualizer-D 配置vite.config.js文件【加入插件】import{defineConfig}from'vite'importvuefrom'@vitejs/plugin-vue'......
  • vue3-vite-cesium
     cesium中文网http://cesium.xin/ 优秀的学习资源http://cesium.xin/wordpress/archives/130.html  vite-plugin-cesiumvite社区插件安装cesiumhttps://git......
  • vue3请求编写规范
    vue3请求编写规范使用的是模块化的组件式API界面request(请求文件夹)总文件夹中包含了对应的接口文件xxxx.ts包含了管理最底层请求的request.ts包含了......
  • vite.config.ts 配置
    import{fileURLToPath,URL}from'node:url'import{defineConfig}from'vite'importvuefrom'@vitejs/plugin-vue'//https://vitejs.dev/config/export......