vue webapp项目中图片上传组件
2019-03-21 12:52
483 查看
<template> <div class="upload-img common-padding"> <ul class="business-type no-margin" v-if="showTitle"> <li class="list "> <label>{{title}}</label> </li> </ul> <div class="box"> <template v-if="imageListValue instanceof Array && imageListValue.length > 0"> <div class="upload-text" v-for="(item, index) in imageListValue" :key="index" @click="removeImg(item)"> <img :src="publicHost + item.attachmentPath" class="result-img"/> </div> </template> <div class="receive-img" v-if="(Object.prototype.toString.call(imageListValue) === '[object Object]') && Object.keys(imageListValue).length !== 0" @click="removeImg()"> <img :src="publicHost + imageListValue.attachmentPath" class="result-img"/> </div> <label> <div class="upload-text" v-if="showAdd"> <img src="./../../../static/img/common/add.png" class="add-plus"/> </div> <slot name="upload-img" v-if="!(imageListValue instanceof Array) && !imageListValue"></slot> <input type="file" capture="camera" accept="image/png,image/gif,image/jpeg" class="hidden" @change="getBaseUrl($event)"/> </label> </div> </div> </template> <script> import { MessageBox } from 'mint-ui' import {uploadFileByBase64Str} from '@/api/file' export default{ data () { return { image: null, temp: [] } }, props: { title: { default: '标题', type: String }, imageList: {}, showAdd: { type: Boolean, default: true }, showTitle: { type: Boolean, default: true } }, computed: { imageListValue: { get: function () { return this.imageList }, set: function (newValue) { this.$emit('update:imageList', newValue) } }, publicHost () { return this.$store.state.dictionary.publicHost + '/' } }, components: {}, methods: { removeImg (url) { MessageBox.confirm('是否删除该图片?').then(action => { if (this.imageListValue instanceof Array) { this.imageListValue.map(val => { if (val.attachmentPath === url.attachmentPath) { this.imageListValue.splice(this.imageListValue.indexOf(val), 1) this.$emit('deleteImg', val) } }) } else { this.imageListValue = {} } }) }, getBaseUrl (e) { const file = e.target.files[0] const reader = new FileReader() reader.readAsDataURL(file)// 读出 base64 let _this = this reader.onloadend = function () { const dataURL = reader.result const data = { fileName: file.name, fileStr: dataURL } uploadFileByBase64Str(data).then(res => { if (res.code === 200) { const attachmentUrl = res.result[0] if (_this.imageListValue instanceof Array) { _this.imageListValue.push(attachmentUrl) } else { _this.imageListValue = attachmentUrl } } }) } } } } </script> <style lang="less"> #fileInput { display: none; } .box{ .img-preview{ display: none; } } </style> <style lang='less' scoped> @import "./../../public/comment"; .hidden{ display: none; } .upload-img{ padding-bottom: 10px/2; margin-bottom: 10px/2; background-color: #fff; .business-type{ margin: 20px auto; } .no-margin{ margin: 0; } .upload-text{ background: #F2F2F2; width: 160px/2; height: 160px/2; border-radius: 2px; display: flex; align-items: center; justify-content: center; margin: 0 10px/2 20px/2; .result-img{ width: 100%; height: 100%; } .add-plus{ width: 60%; } } .receive-img{ height: 160px/2; img{ height: 100%; } } } .box{ display: flex; flex-wrap: wrap; } .list{ width: 100%; height: 104px/2; display: flex; align-items: center; font-size: 32px/2; color: #333333; background-color: #fff; margin-bottom: 5px/2; label{ width: 260px/2; } .text-1{ font-size: 28px/2; color: #999999; } .text-2{ color: @lightBlue; } } </style>
相关文章推荐
- hybird app项目实例:安卓webview中HTML5拍照图片上传
- 关于intellij14的web项目导入lib丢失以及上传资源图片等无法访问的问题
- 解决 react native 的 webview 组件不支持android客户端上传图片文件问题
- webapp图片滑动组件
- vue多张上传图片的组件
- java web项目图片上传
- Vue+Webpack+node构建web和App项目
- vue+elementUi做的图片上传组件
- vue项目优化之按需加载组件-使用webpack require.ensure
- web项目中,上传到tomcat的图片被删除的原因
- Vue上传图片预览以及删除的vue组件
- JAVA WEB 上传图片文件到工程项目下
- Java-Maven项目引入UEditor图片上传组件jar包类库的5种方式
- vue打包项目,webpack配置改变以及图片正确引入姿势
- 图片上传至Web项目下指定文件夹,需刷新项目下文件夹才能显示(已解决)
- Vue+Router+Webpack+Axios 构建项目实战(三)调整 App.vue 和 router 路由
- 在vue项目中使用element-ui的Upload上传组件
- Vue webapp项目通过HBulider打包原生APP(vue+webpack+HBulider)
- 关于web项目中的图片上传、并在前端显示问题(tomcat中配置文件上传虚拟路径)
- web项目利用apache的DiskFileItemFactory和ServletFileUpload上传图片