图片上传与浏览 - 图1

    1. <template>
    2. <div>
    3. <el-upload class="uploader" :headers="authorToken" :action="UPLOAD_URL" :auto-upload="true"
    4. :file-list="fileList" list-type="picture-card" :limit="limit?limit:9" :accept="fileType?fileType:'image/*'"
    5. :on-success="handleGoodsImagesUploadSuccess" :before-upload="handlebeforeUpload" :multiple="true"
    6. ref="fileupload" :on-exceed="handleUploadExceed" :on-remove="handleRemove"
    7. :on-preview="handlePictureCardPreview">
    8. <i class="el-icon-plus"></i>
    9. </el-upload>
    10. <el-dialog :append-to-body="true" :visible.sync="dialogImgVisible" style="z-index: 3000;text-align: center;"
    11. :close-on-click-modal="false" :close-on-press-escape="false" custom-class="pub_dialog">
    12. <img width="80%" :src="dialogImageUrl" alt="" />
    13. </el-dialog>
    14. </div>
    15. </template>
    16. <!--
    17. 使用用例
    18. fileType可以是
    19. audio/*表示“任何音频文件
    20. video/*表示“任何视频文件
    21. image/*表示“任何图像文件
    22. 还可以以逗号拼接,如image/*,.pdf
    23. <multi-upload v-model="form.voucherUrlList" :fileType="image/*"></multi-upload>
    24. import MultiUpload from '@/components/Upload/MultiUpload'
    25. components: {
    26. MultiUpload
    27. },
    28. -->
    29. <script>
    30. // import {
    31. // UPLOAD_URL
    32. // } from '../../utils/api.js';
    33. export default {
    34. props: {
    35. value: {
    36. type: Array,
    37. default: () => []
    38. },
    39. limit: {
    40. type: Number,
    41. default: 9
    42. },
    43. fileType: String,
    44. },
    45. data() {
    46. return {
    47. authorToken: {
    48. 'Authorization': 'Bearer ' + sessionStorage.getItem("token")
    49. },
    50. UPLOAD_URL: "/v1/admin/common/upload", // 上传的图片服务器地址 即上传图片后台接口的路径
    51. loading: '',
    52. param: {
    53. token: ''
    54. },
    55. dialogImageUrl: "",
    56. dialogImgVisible: false,
    57. disabled: false,
    58. imgs:[],
    59. num:0,
    60. fileNum:0
    61. }
    62. },
    63. computed: {
    64. // ['xxx', 'xxx'] 转换为 [{url: 'xxx'}, {url: 'xxx'}]
    65. fileList() {
    66. return this.value.map(url => ({
    67. url
    68. }))
    69. }
    70. },
    71. methods: {
    72. handlePictureCardPreview: function(file) {
    73. this.dialogImageUrl = file.url;
    74. this.dialogImgVisible = true;
    75. },
    76. handleRemove: function(file, fileList) {
    77. // fileList 为删除后的文件列表
    78. const value = fileList.map(v => v.url)
    79. this.$emit('input', value)
    80. },
    81. handleGoodsImagesUploadSuccess(response, file, fileList) {
    82. console.log(response);
    83. console.log(fileList);
    84. if (response.code == 200) {
    85. let imageUrl = response.data;
    86. this.imgs.push(imageUrl);
    87. this.num++;
    88. if(this.num == this.fileNum){
    89. this.num = 0;
    90. this.fileNum = 0;
    91. // 这里如果 this.value.push(imageUrl) 这么写,vue会报出警告,大概意思是value作为props不应该在子组件中被修改
    92. // 应该根据 value 得到新的值,而不能修改它,this.value.concat(imageUrl)也是可以的,concat方法返回新的数组
    93. // this.$emit('input', [...this.value, imageUrl])
    94. this.$emit('input', this.value.concat(this.imgs))
    95. this.imgs =[];
    96. }
    97. } else {
    98. this.$message.error(file.name + '上传失败!');
    99. }
    100. },
    101. handlebeforeUpload(file) {
    102. // 这里做可以做文件校验操作
    103. const isImg = /^image\/\w+$/i.test(file.type)
    104. if (!isImg && this.fileType == 'image/*') {
    105. this.$message.error('只能上传 JPG、PNG、GIF 格式!')
    106. return false
    107. }
    108. this.fileNum++;
    109. console.log(this.fileNum);
    110. },
    111. handleUploadExceed() {
    112. this.$message.error(`最多上传${this.limit}张图片`)
    113. },
    114. }
    115. }
    116. </script>
    117. <style>
    118. /*去除upload组件过渡效果*/
    119. .el-upload-list__item {
    120. transition: none !important;
    121. }
    122. </style>
    123. <style scoped lang="scss">
    124. .hide>>>.el-upload--picture-card {
    125. display: none;
    126. }
    127. </style>