不用插件实现vue移动端上传图片和回显图片,开箱即用的移动端上传图片

不用插件实现vue移动端上传图片和回显图片,

贴入一段html代码,比较乱,但是可以开箱即用,样式可以自行调改,里面的img是我从阿里矢量图里当下来的 可以自行查找

<input @change="fileChange($event)" type="file" id="upload_file" multiple style="display: none"/>
							<div class="add" @click="chooseType">
								<div class="add-image" align="center" style="    position: relative;top: 17px;left: -8px;">
									<img src="/demo/static/img/yewuxitong/alijiahao.png" style="width: 75px;position: relative;    top: -11px;">
<!--									<p class="font13">添加图片</p>-->
								</div>
							</div>
							<div class="add-img" v-show="imgList.length" style="    position: relative;left:-11px">
								<p class="font14" style="    position: absolute;    bottom: -14px;">图片(最多6张,还可上传<span v-text="6-imgList.length"></span>)</p>
								<ul class="img-list">
									<li v-for="(url,index) in imgList">
										<img class="del" src="/demo/static/img/cha.png" @click.stop="delImg(index)" style="    width: 25px;position: absolute;left: 71px;top: 17px;"/>
										<img :src="url.file.src" style="    width: 97px;height: 97px;position: absolute;top: 16px;left: -1px;">
									</li>
								</ul>
							</div>

定义data里的数据

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ms1lwj2s-1629872474604)(C:\Users\xiaoxinxin\AppData\Roaming\Typora\typora-user-images\image-20210825141807277.png)]

methods里的方法

	methods: {
					chooseType() {
						document.getElementById('upload_file').click();
					},
					fileChange(el) {
						if (!el.target.files[0].size) return;
						this.fileList(el.target);
						el.target.value = ''
					},
					fileList(fileList) {
						let files = fileList.files;
						for (let i = 0; i < files.length; i++) {
							//判断是否为文件夹
							if (files[i].type != '') {
								this.fileAdd(files[i]);
							} else {
								//文件夹处理
								this.folders(fileList.items[i]);
							}
						}
					},
					//文件夹处理
					folders(files) {
						let _this = this;
						//判断是否为原生file
						if (files.kind) {
							files = files.webkitGetAsEntry();
						}
						files.createReader().readEntries(function (file) {
							for (let i = 0; i < file.length; i++) {
								if (file[i].isFile) {
									_this.foldersAdd(file[i]);
								} else {
									_this.folders(file[i]);
								}
							}
						});

					},
					foldersAdd(entry) {
						let _this = this;
						entry.file(function (file) {
							_this.fileAdd(file)
						})
					},
					fileAdd(file) {
						if (this.limit !== undefined) this.limit--;
						if (this.limit !== undefined && this.limit < 0) return;
						//总大小
						this.size = this.size + file.size;
						//判断是否为图片文件
						if (file.type.indexOf('image') == -1) {
							this.$dialog.toast({mes: '请选择图片文件'});
						} else {
							let reader = new FileReader();
							let image = new Image();
							let _this = this;
							reader.readAsDataURL(file);
							reader.onload = function () {
								file.src = this.result;
								image.onload = function(){
									let width = image.width;
									let height = image.height;
									file.width = width;
									file.height = height;
									_this.imgList.push({
										file
									});
									console.log( _this.imgList);
								};
								image.src= file.src;
							}
						}
					},
					delImg(index) {
						this.size = this.size - this.imgList[index].file.size;//总大小
						this.imgList.splice(index, 1);
						if (this.limit !== undefined) this.limit = 6-this.imgList.length;
					},
					displayImg() {

					}
				},

效果图

效果图

[外链图片转存中...(img-rP05nxlC-1629872474607)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mangoxin1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值