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 ...
随机推荐
- 开发工具使用:CubeMX、KEIL MDK-ARM
来源:成电<微机原理与嵌入式系统>漆强 第四章 STM32CubeMX软件的使用 来源:成电<微机原理与嵌入式系统>漆强 第五章 MDK-ARM软件的使用 一.STM32的Cu ...
- SpringBoot项目启动过程动态修改接口请求路径
背景 最近遇到一个技术需求,需要对其他多个已有的服务进行整合打包为一个整体的服务,项目启动过程发现一个问题,在controller层多个服务之间存在相同的RequestMapping接口请求路径,导致 ...
- 媒体img组件以及swiper轮播
.swiper{ height: 400rpx; margin-top: 100rpx; .item{ padding: 20rpx; ...
- C++学习笔记一:windows系统配置C++开发环境(VS code+g++/clang++)
1.下载vscode 官网下载地址: https://code.visualstudio.com/ 安装时选择把软件加入到环境变量中这个选项 2.打开vscode,安装c/c++扩展插件 3.下载gc ...
- Java8新特性Stream流
1.是什么? Stream(流)是一个来自数据源的元素队列并支持聚合操作 2.能干嘛? Stream流的元素是特定类型的对象,形成一个队列. Java中的Stream并不会存储元素,而是按需计算. 数 ...
- 华企盾DSC远程桌面、实时监控连接不上常见处理方法
1.检测策略是否勾选了远程桌面需要客户端确认或者勾选了客户端显示"允许远程维护"选项 2.检查客户端5097目录是否有MgrDll.dll.uvnserver.exe文件(是否被杀 ...
- TDD、BDD、ATDD都是什么、有什么区别?(下)
在<TDD.BDD.ATDD都是什么.有什么区别?(下)>一文中,探讨了TDD.BDD和ATDD的概念.虽然TDD.BDD和ATDD都是软件开发中使用的测试方法,但它们在方法和重点上有所不 ...
- IDM HOSTS本地注册 屏蔽的网址
127.0.0.1 registeridm.com127.0.0.1 www.registeridm.com127.0.0.1 www.internetdownloadmanager.com127.0 ...
- 【eBPF-03】进阶:BCC 框架中 BPF 映射的应用 v1.0
eBPF 中实现内核态代码与用户态代码是可以实时通信的,这主要靠 BPF 映射 来实现. BPF 映射 是内核空间的一段内存,以 键值对 的方式存储.内核态程序可以直接访问 BPF 映射,用户态需要通 ...
- Prometheus 与 VictoriaMetrics对比
公众号「架构成长指南」,专注于生产实践.云原生.分布式系统.大数据技术分享 时序数据库有很多,比如Prometheus.M3DB.TimescaleDB.OpenTSDB.InfluxDB等等.Pro ...