当前位置:   article > 正文

笔记-vue 自定义上传组件_vue 文件上传组件

vue 文件上传组件

一、UploadFile文件夹下建index.vue

  1. <template>
  2. <div>
  3. <div class="img-up">
  4. <div
  5. class="img-list"
  6. @mouseenter="imgShow = index"
  7. @mouseleave="imgShow = ''"
  8. v-for="(item, index) in fileList"
  9. :key="index"
  10. >
  11. <img :src="item.url" class="avatar" />
  12. <transition name="el-zoom-in-bottom">
  13. <div class="img-tools" v-show="imgShow === index">
  14. <i class="el-icon-view img-view" @click="handlePreview(item)" ></i>
  15. <i class="el-icon-delete img-del" @click="beforeRemove(item)"></i></div
  16. ></transition>
  17. </div>
  18. <el-upload
  19. action="#"
  20. ref="upload"
  21. accept="image/*"
  22. :http-request="handleFileUpload"
  23. :on-preview="handlePreview"
  24. :on-remove="handleRemove"
  25. :before-upload="beforeAvatarUpload"
  26. :on-exceed="handleExceed"
  27. :limit="3"
  28. :show-file-list="false"
  29. >
  30. <i v-if="fileList.length < 3" class="el-icon-plus img-btn"></i>
  31. </el-upload>
  32. </div>
  33. <!-- 上传提示 -->
  34. <div class="el-upload__tip" slot="tip" v-if="isShowTip">
  35. 请上传
  36. <template v-if="fileSize"> 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b> </template>
  37. <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template>
  38. 的文件
  39. </div>
  40. <el-dialog
  41. append-to-body
  42. title="图片预览"
  43. :visible.sync="dialogVisible"
  44. width="500px"
  45. style="z-index: 2005;text-align: center;"
  46. >
  47. <img :src="dialogImageUrl" style="width: 100%;" alt="" />
  48. </el-dialog>
  49. </div>
  50. </template>
  51. <script>
  52. //修改此处api
  53. import { fileUpload } from '@/api/system/file'
  54. import { detectPicture } from '@/api/bussiness/clothes'
  55. export default {
  56. props: {
  57. //父组件传入回显参数 ‘1.png,2.png’ 逗号隔开字符串
  58. value: {
  59. type: String,
  60. },
  61. // 大小限制(MB)
  62. fileSize: {
  63. type: Number,
  64. default: 5,
  65. },
  66. // 文件类型, 例如['png', 'jpg', 'jpeg']
  67. fileType: {
  68. type: Array,
  69. default: () => ["png", "jpg", "jpeg"],
  70. },
  71. // 是否显示提示
  72. isShowTip: {
  73. type: Boolean,
  74. default: true
  75. },
  76. interfaceType: {
  77. type: String,
  78. default: 'normal'
  79. }
  80. },
  81. data() {
  82. return {
  83. loading: false,
  84. fileList: [], //深拷贝,判断重名及第一步时的文件信息展示
  85. // baseUrl: APP_PUBLIC_CONFIG.IMG_IRL,//全局变量。拼接图片前缀
  86. dialogImageUrl: "",
  87. dialogVisible: false,
  88. imgShow: "",
  89. };
  90. },
  91. watch: {
  92. value(v) {
  93. this.fileList = this.getFlieList(v);
  94. },
  95. deep: true,
  96. immediate: true
  97. },
  98. mounted() {},
  99. methods: {
  100. //过滤回显参数,将字符串转为数组,不然不能回显
  101. getFlieList(list) {
  102. console.log('list',list)
  103. if (!list) return [];
  104. return list.split(",").map((e) => {
  105. return {
  106. name: e,
  107. url: e,
  108. };
  109. });
  110. },
  111. beforeAvatarUpload(file) {
  112. if (this.fileList > 3) {
  113. this.handleExceed(this.fileList)
  114. return
  115. }
  116. const isLt2M = file.size / 1024 / 1024 < 3;
  117. const isJPG = ["image/jpeg", "image/png"];
  118. if (!isLt2M) {
  119. this.$message.error("上传图片大小不能超过 3MB!");
  120. return false;
  121. }
  122. if (!isJPG.includes(file.type)) {
  123. this.$message.error("上传图片只能是 JPG和PNG 格式!");
  124. return false;
  125. }
  126. },
  127. // 处理移除操作
  128. handleRemove(file, fileList) {
  129. let uid = file.uid // 关键作用代码,去除文件列表失败文件
  130. let idx = this.$refs.upload.uploadFiles.findIndex(item => item.uid === uid)
  131. this.$refs.upload.uploadFiles.splice(idx, 1)
  132. this.fileList = this.fileList.filter((e) => e.uid != file.uid).map((e) => ({url:e.url,uid:e.uid,name:e.name}))
  133. this.$emit("upSuccess", this.fileList.filter((e) => e.uid != file.uid).map((e) => e.url).toString());
  134. },
  135. // 处理预览操作
  136. handlePreview(file) {
  137. console.log(file)
  138. this.dialogImageUrl = file.url;
  139. this.dialogVisible = true;
  140. },
  141. // 处理超出图片个数操作
  142. handleExceed(files ) {
  143. this.$message.warning(
  144. `当前限制选择 3 个文件,本次选择了 ${files.length} 个文件 `
  145. );
  146. },
  147. // 移除之前的操作
  148. beforeRemove(file, fileList) {
  149. return this.$confirm(`确定移除 ${file.name}?`).then(_ => {
  150. this.handleRemove(file)
  151. });
  152. },
  153. // 处理文件上传操作
  154. handleFileUpload(file) {
  155. this.loading = true;
  156. let randomData = Math.random().toString(10).slice(-10)
  157. let form = new FormData();
  158. form.append("file", file.file);
  159. form.append("requestId",randomData);
  160. fileUpload(form).then((res) => {
  161. let param = res.data;
  162. // 正常接口
  163. if(this.interfaceType === 'normal') {
  164. this.fileList.push({ url: param, name: file.file.name });
  165. //返回上传后的图片数据
  166. this.$emit("upSuccess", this.fileList.map((e) => e.url).toString());
  167. this.loading = false;
  168. }
  169. // 工服
  170. if(this.interfaceType === 'workCloth') {
  171. detectPicture({pictureUrl:res.data}).then(response => {
  172. console.log('code',response.code)
  173. if(response.code === 200) {
  174. this.fileList.push({ url: param, name: file.file.name,uid:file.file.uid });
  175. //返回上传后的图片数据
  176. this.$emit("upSuccess", this.fileList.map((e) => e.url).toString());
  177. this.loading = false;
  178. }
  179. }).catch(error => {
  180. let uid = file.file.uid // 关键作用代码,去除文件列表失败文件
  181. let idx = this.$refs.upload.uploadFiles.findIndex(item => item.uid === uid)
  182. this.$refs.upload.uploadFiles.splice(idx, 1)
  183. })
  184. }
  185. });
  186. },
  187. },
  188. };
  189. </script>
  190. <style scoped lang="scss">
  191. .img-up {
  192. display: flex;
  193. .avatar,
  194. .img-btn {
  195. width: 100px;
  196. height: 100px;
  197. margin-right: 10px;
  198. }
  199. .img-list {
  200. position: relative;
  201. .img-tools {
  202. width: 100px;
  203. height: 100px;
  204. background: rgba($color: #000000, $alpha: 0.5);
  205. position: absolute;
  206. left: 0;
  207. top: 0;
  208. display: flex;
  209. align-items: center;
  210. justify-content: center;
  211. }
  212. .img-del,.img-view {
  213. color: #fff;
  214. font-size: 20px;
  215. cursor: pointer;
  216. }
  217. .img-view{
  218. margin-right: 15px;
  219. }
  220. }
  221. }
  222. .img-btn {
  223. width: 100px;
  224. height: 100px;
  225. border: 1px dashed #dcdfe6;
  226. display: flex;
  227. justify-content: center;
  228. align-items: center;
  229. font-size: 30px;
  230. color: #dcdfe6;
  231. }
  232. .transition-box {
  233. margin-bottom: 10px;
  234. width: 200px;
  235. height: 100px;
  236. border-radius: 4px;
  237. background-color: #409EFF;
  238. text-align: center;
  239. color: #fff;
  240. padding: 40px 20px;
  241. box-sizing: border-box;
  242. margin-right: 20px;
  243. }
  244. </style>

二、页面引用

  1. <template>
  2. <UploadFile ref="uploadFile" :fileSize="3" :value="form.picture" @upSuccess="(e) => {form.picture = e;}"/>
  3. <template />
  4. <script>
  5. import UploadFile from "@/components/UploadFile";
  6. export default {
  7. components: { UploadFile },
  8. data() {
  9. //这里存放数据
  10. return {
  11. form: {
  12. faceLibPersonList: [],
  13. picture: '',
  14. }
  15. }
  16. }
  17. }
  18. <script/>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/995785
推荐阅读
相关标签
  

闽ICP备14008679号