vue vantUI实现文件(图片、文档、视频、音频)上传(多文件)
上传文档格式
1 <template>
2 <div>
3 <div class="upload">
4 <div>
5 <div
6 class="forPreview_doc"
7 v-for="(item, index) in uploadDocList"
8 :key="index"
9 >
10 <img src="../../assets/img/resource_doc_b@2x.png" alt="" />
11 <span>{{ item.name }}</span>
12 <span>{{ item.size | formatSize }}</span>
13 <van-icon name="delete" @click="delBtn(index)" class="delte" />
14 </div>
15 <van-uploader
16 v-show="uploadDocList.length < 2"
17 preview-size="80px"
18 accept=".doc, .docx, .xml, .xlsx, .pdf"
19 :before-read="beforeRead"
20 :after-read="afterRead"
21 ></van-uploader>
22 </div>
23
24 <div class="diy-submit">
25 <van-button @click="doSubmit($event)">提交</van-button>
26 </div>
27 </div>
28 </div>
29 </template>
30
31 <script>
32 import { uploadFile, addResources } from "../../http/mock";
33 import Toast from "vant";
34 export default {
35 name: "uploadFile",
36
37 data() {
38 return {
39 tagList: [],
40 uploadTitle: "",
41 currentTag: null,
42 tagId: null,
43 columnName: localStorage.getItem("columnName"),
44 fileIdArr: [],
45
46 uploadDocList: [], // 选中的上传文档列表
47 };
48 },
49 filters: {
50 formatSize(val) {
51 // 格式化文件大小
52 if (val > 0) {
53 return (val / 1024 / 1024).toFixed(2) + "M";
54 } else {
55 return "0M";
56 }
57 },
58 },
59 methods: {
60 // vant上传文件前校验文件事件
61 // 文件选中后先提交给后台,后台根据选中的文件,返回数组(这一业务根据后台而定)
62 beforeRead(file) {
63 let formData = new FormData(); // 为上传文件定义一个formData对象
64 let config = {
65 headers: {
66 "Content-Type": "multipart/form-data",
67 },
68 };
69 this.uploadDocList.push(file);
70 this.uploadDocList.forEach((item) => {
71 formData.append(item.name, item);
72 });
73
74 // formData.append(file.name, file); // 单个文件上传时可以这么写,上面的foreach就可以删掉
75 this.$api
76 .post(uploadFile, formData, config)
77 .then((res) => {
78 this.fileIdArr = res.data.data; // 把選中的文件傳送給後台
79 })
80 .catch((err) => {
81 Toast("文件上傳失敗!");
82 });
83 },
84 // 删除待上传的文件
85 delBtn(index) {
86 if (isNaN(index) || index >= this.uploadDocList.length) {
87 return false;
88 }
89 let tmp = [];
90 for (let i = 0; i < this.uploadDocList.length; i++) {
91 if (this.uploadDocList[i] !== this.uploadDocList[index]) {
92 tmp.push(this.uploadDocList[i]);
93 }
94 }
95 this.uploadDocList = tmp;
96 },
97 doSubmit() {
98 let params = {
99 classify: this.tagId, // 针对视频资源时对应的分类id
100 file: this.fileIdArr, // 选择完文件后,调用uploadFile这个接口,后端返回的数组
101 resourceColumnId: JSON.parse(localStorage.getItem("columnId")), // 资源栏目id(视频、图片、音频、文档)
102 title: this.uploadTitle, // 上传时填写的标题
103 };
104
105 this.$api
106 .post(addResources, params)
107 .then((res) => {
108 if (res.data.code === 1001) {
109 this.$router.push({ name: "myResourceClassify" });
110 }
111 })
112 .catch((err) => {
113 console.log(err);
114 });
115 },
116 },
117 mounted() {},
118 };
119 </script>
120 <style lang="less" scoped>
121 .upload {
122 padding: 140px 36px 160px 36px;
123 box-sizing: border-box;
124 }
125
126 .forPreview_video {
127 position: relative;
128 /*background: rgba(0,0,0,.7);*/
129 video {
130 width: 95%;
131 max-height: 430px;
132 }
133 .delte {
134 position: absolute;
135 right: 0;
136 bottom: 0;
137 }
138 }
139 .forPreview_doc,
140 .forPreview_audio {
141 display: flex;
142 margin-bottom: 10px;
143 align-items: center;
144 img {
145 width: 56px;
146 height: 56px;
147 margin-right: 20px;
148 }
149 span:nth-of-type(1) {
150 flex: 1;
151 }
152 span:nth-of-type(2) {
153 margin-right: 20px;
154 }
155 }
156 .forPreview_pic {
157 display: flex;
158 align-items: flex-end;
159 img {
160 width: 160px;
161 height: 160px;
162 }
163 }
164
165 .diy-detail {
166 width: 100%;
167 overflow: hidden;
168
169 .btn {
170 span {
171 margin-bottom: 10px;
172 }
173 }
174 .van-cell {
175 background-color: #f0f0f0;
176 border-radius: 35px;
177 font-size: 26px;
178 height: 69px;
179 line-height: 69px;
180 padding: 0 22px;
181 color: #999;
182 }
183 .van-hairline--top-bottom::after,
184 .van-hairline-unset--top-bottom::after {
185 border-width: 0;
186 }
187 p {
188 height: 64px;
189 line-height: 64px;
190 font-size: 32px;
191 color: #333;
192 position: relative;
193 padding-left: 16px;
194 }
195 p::before {
196 position: absolute;
197 top: 0;
198 left: 0;
199 content: "*";
200 color: #ff0000;
201 }
202
203 span {
204 display: inline-block;
205 width: 157px;
206 background: #f0f0f0;
207 border-radius: 35px;
208 color: #999;
209 font-size: 26px;
210 padding: 14px 18px;
211 margin-right: 28px;
212 text-align: center;
213 }
214 .active {
215 color: #fff;
216 background: linear-gradient(to right, #fd5130, #fa6c34);
217 }
218 }
219 .diy-submit {
220 position: fixed;
221 height: 150px;
222 width: 90%;
223 bottom: 0;
224 background: #fff;
225
226 .van-button {
227 width: 100%;
228 height: 90px;
229 border-radius: 45px;
230 font-size: 36px;
231 color: #fff;
232 background: linear-gradient(to right, #fd5130, #fa6c34);
233 top: 50%;
234 transform: translate(0, -50%);
235 }
236 .van-button--default {
237 border: none;
238 }
239 }
240 </style>
https://www.jb51.net/article/171972.htm
vue vantUI实现文件(图片、文档、视频、音频)上传(多文件)的更多相关文章
- 跟我学SharePoint 2013视频培训课程——怎样创建文档库并上传文档(8)
课程简介 第8天,怎样在SharePoint 2013怎样创建文档库并上传文档. 视频 SharePoint 2013 交流群 41032413
- 自动把动态的jsp页面(或静态html)生成PDF文档,并且上传至服务器
置顶2017年11月06日 14:41:04 阅读数:2311 这几天,任务中有一个难点是把一个打印页面自动给生成PDF文档,并且上传至服务器,然而公司框架只有手动上传文档,打印时可以保存为PDF在本 ...
- DEDECMS:解决BMP、jpeg图片或MP4视频无法上传和在后台无法显示
一.BMP图片无法上传解决方法: 1.修改配置文件: 在include-->dialog的文件夹下, select_images_post.php--> 把 $sparr = Array( ...
- Swagger文档添加file上传参数写法
想在swagger ui的yaml文档里面写一个文件上传的接口,找了半天不知道怎么写,终于搜到了,如下: /tools/upload: post: tags: - "tool" s ...
- react native android 上传文件,Nodejs服务端获取上传的文件
React Native端 使用react-native-image-picker 做出选择图片的操作,选择完成后,直接将图片Post至服务器,保存在服务器的某个地方(保存图片的路径需要公开显示),并 ...
- input file实现多次上传文件(不会覆盖上次上传的文件)
html原生的file多选控件:<input class="className" type="file" name="name" ac ...
- vue a标签下载图片文档显示下载失败
解决:把所要下载的文件放到static文件下,具体原因-静态文件放在static内,否则webpack会打包.
- git中 .ignore文件的配置 忽略不想上传的文件
1.配置语法: 以斜杠“/”开头表示目录: 以星号“*”通配多个字符: 以问号“?”通配单个字符 以方括号“[]”包含单个字符的匹配列表: 以叹号“!”表示不忽略(跟踪)匹配到的文件或目录: 此外,g ...
- ASP.NET Core 上传多文件 超简单教程
示例源码下载地址 https://qcloud.coding.net/api/project/3915794/files/4463836/download 项目地址 https://dev.tence ...
- jmert中如何测试上传文件接口(测试上传excel文件)
第一次用jmeter这个工具测试上传接口,以前没做过这一块,导致走了很多弯路.特地把经验谢谢,怕自己以后忘记... 一,jmeter如何上传文件 jmeter 的 http requests post ...
随机推荐
- 城院导航小程序软件需求规范(SRS)(三期作业汇总)
城院导航小程序软件需求规范(SRS) 1. 引言 1.1 目的 小帅小美们注意看!! 公主请批阅! 王子请批阅! 本文档描述了城院导航小程序的功能和非功能需求.它旨在为开发团队.导师和利益相关者提供清 ...
- antd Pro组件ProFormList实现自定义action
antd Pro组件ProFormList实现自定义action ProFormList是ant design pro的结构化数据组件,通常用来实现动态表单. 现在有个需求,除了组件自带的删除和复制, ...
- Springboot的Container Images,docker加springboot
Spring Boot应用程序可以使用Dockerfiles容器化,或者使用Cloud Native Buildpacks来创建优化的docker兼容的容器映像,您可以在任何地方运行. 1. Effi ...
- Protobuf的使用,结合idea
安装Protobuf并配置idea Protocol Buffers(又名 protobuf)是 Google 的中立语言, 平台中立.可扩展的结构化数据序列化机制. 官网: https://gith ...
- LCIS最长公共上升子序列!HDU-1423
This is a problem from ZOJ 2432.To make it easyer,you just need output the length of the subsequence ...
- 专业的字节码查看工具——jclasslib
下载地址: GitHub地址:点击下方蓝色链接即可. https://github.com/ingokegel/jclasslib/releases/download/6.0.4/jclasslib_ ...
- Python——第一章:注释、变量、常量
python中的注释有2种: 1.单行注释 单行注释用# #这是一个单行注释 快捷键用Ctrl+/全选多个内容可以多行快速注释,也可以快速去掉注释符# 比如快速将全选的所有行注释掉--加# 2.多行注 ...
- history详解
linux下history命令详解 如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率.本文将通过实例的方式向你介绍 history 命令的 15 个用法. ...
- 文心一言 VS 讯飞星火 VS chatgpt (177)-- 算法导论13.3 6题
六.用go语言,说明如果红黑树的表示中不提供父指针,应当如何有效地实现 RB-INSERT. 文心一言: 红黑树是一种自平衡的二叉搜索树,其中每个节点都包含一个颜色属性(红色或黑色),并且满足以下性质 ...
- 2023-09-05:请用go语言编写。一个图像有n个像素点,存储在一个长度为n的数组arr里, 每个像素点的取值范围[0,s]的整数, 请你给图像每个像素点值加上一个整数k(可以是负数), 像素值会
2023-09-05:请用go语言编写.一个图像有n个像素点,存储在一个长度为n的数组arr里, 每个像素点的取值范围[0,s]的整数, 请你给图像每个像素点值加上一个整数k(可以是负数), 像素值会 ...