uploadImage.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <div id="uploadImage">
  3. <el-upload
  4. v-if="url"
  5. ref="upload"
  6. :action="url"
  7. :list-type="listType"
  8. :file-list="fileList"
  9. :limit="limit"
  10. :on-exceed="outLimit"
  11. :on-preview="handlePictureCardPreview"
  12. :on-remove="handleRemove"
  13. :on-success="onSuccess"
  14. :show-file-list="showList"
  15. :accept="accept"
  16. >
  17. <el-button size="small" class="isBtn" v-if="isBtn">点击上传文件</el-button>
  18. <template v-else>
  19. <i class="el-icon-plus"></i>
  20. </template>
  21. <template #tip v-if="tip">
  22. <span class="tip">{{ tip }}</span>
  23. </template>
  24. </el-upload>
  25. <el-dialog
  26. :visible.sync="dialogVisible"
  27. width="30%"
  28. >
  29. <img width="100%" :src="dialogImageUrl" alt="" />
  30. </el-dialog>
  31. </div>
  32. </template>
  33. <script>
  34. export default {
  35. name: 'uploadImage',
  36. props: {
  37. url: { type: null },
  38. limit: { type: Number },
  39. data: { type: null },
  40. isBtn: { type: Boolean, default: false },
  41. showList: { type: Boolean, default: true },
  42. accept: { type: String },
  43. tip: { type: String, default: undefined },
  44. listType: { type: String, default: 'picture-card' },
  45. },
  46. components: {},
  47. data: () => ({
  48. dialogVisible: false,
  49. dialogImageUrl: '',
  50. fileList: [],
  51. }),
  52. created() {
  53. },
  54. mounted(){
  55. },
  56. watch: {
  57. data: {
  58. handler(val) {
  59. this.defaultProcess(val);
  60. },
  61. deep: true,
  62. immediate: true,
  63. },
  64. },
  65. computed: {},
  66. methods: {
  67. handlePictureCardPreview(file) {
  68. /*let eleLink = document.createElement('a');
  69. eleLink.setAttribute("download", file.name);
  70. eleLink.href = file.url;
  71. eleLink.style.display = 'none';
  72. document.body.appendChild(eleLink);
  73. eleLink.click();
  74. document.body.removeChild(eleLink);*/
  75. this.dialogImageUrl = file.url;
  76. this.dialogVisible = true;
  77. },
  78. handleRemove(file) {
  79. this.$emit('remove', file);
  80. },
  81. outLimit() {
  82. this.$message.error(`只允许上传${this.limit}个文件`);
  83. },
  84. onSuccess(response, file, fileList) {
  85. this.$emit('uploadSuccess', {data: response });
  86. },
  87. defaultProcess(val) {
  88. if(val){
  89. this.$set(this, 'fileList', [{name:val,url:val}]);
  90. }else {
  91. this.$set(this, 'fileList', []);
  92. }
  93. },
  94. },
  95. };
  96. </script>
  97. <style lang="less" scoped>
  98. .isBtn {
  99. background-color: #e9021d;
  100. color: #ffffff;
  101. }
  102. .links {
  103. margin: 0 0 0 15px;
  104. }
  105. .tip {
  106. padding: 0 10px;
  107. display: inline-block;
  108. color: #999999;
  109. }
  110. </style>