uploadraw.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <div id="upload">
  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. :before-remove="handleRemove"
  13. :on-success="onSuccess"
  14. :before-upload="beforeUpload"
  15. :show-file-list="showList"
  16. accept=".3gpp,.ac3,.au,.mp2,.mp3,.mp4,.mpeg,.mpg,.wmv,.avi,.flv"
  17. >
  18. <el-button size="small" type="primary" v-if="isBtn">点击上传</el-button>
  19. <template v-else-if="uploadBtn">
  20. <el-button type="danger">选择文件</el-button>
  21. </template>
  22. <template v-else>
  23. <el-button type="primary" size="mini">选择文件</el-button>
  24. </template>
  25. <template #tip v-if="tip">
  26. {{ tip }}
  27. </template>
  28. </el-upload>
  29. <el-dialog :visible.sync="dialogVisible">
  30. <img width="100%" :src="dialogImageUrl" alt="" />
  31. </el-dialog>
  32. </div>
  33. </template>
  34. <script>
  35. import _ from 'lodash';
  36. export default {
  37. name: 'upload',
  38. props: {
  39. url: { type: null },
  40. limit: { type: Number },
  41. data: { type: null },
  42. type: { type: String },
  43. isBtn: { type: Boolean, default: false },
  44. uploadBtn: { type: Boolean, default: false },
  45. showList: { type: Boolean, default: true },
  46. tip: { type: String, default: undefined },
  47. listType: { type: String, default: 'picture-card' },
  48. },
  49. components: {},
  50. data: () => ({
  51. dialogVisible: false,
  52. dialogImageUrl: '',
  53. fileList: [],
  54. }),
  55. created() {
  56. if (this.data) {
  57. this.defalutProcess(this.data);
  58. }
  59. },
  60. watch: {
  61. data: {
  62. handler(val) {
  63. this.defalutProcess(val);
  64. },
  65. },
  66. },
  67. computed: {},
  68. methods: {
  69. handlePictureCardPreview(file) {
  70. this.dialogImageUrl = file.url;
  71. this.dialogVisible = false;
  72. },
  73. handleRemove(file, fileList) {
  74. this.$set(this, `fileList`, []);
  75. // let index = fileList.findIndex(f => _.isEqual(f, file));
  76. // this.$emit('delete', index);
  77. // return false;
  78. },
  79. outLimit() {
  80. this.$message.error(`只允许上传${this.limit}个文件`);
  81. },
  82. onSuccess(response, file, fileList) {
  83. //将文件整理好传回父组件
  84. this.$emit('upload', { type: this.type, data: { ...response, name: file.name } });
  85. },
  86. beforeUpload(file) {
  87. if (
  88. file.type == 'video/mp4' ||
  89. file.type == 'video/3gpp' ||
  90. file.type == 'video/ac3' ||
  91. file.type == 'video/au' ||
  92. file.type == 'video/mp2' ||
  93. file.type == 'video/mp3' ||
  94. file.type == 'video/mpeg' ||
  95. file.type == 'video/mpg' ||
  96. file.type == 'video/wmv' ||
  97. file.type == 'video/avi' ||
  98. file.type == 'video/flv'
  99. ) {
  100. return true;
  101. } else {
  102. this.$message.error('非影音格式文件/不是可演示文件,不允许上传!');
  103. this.$set(this, `fileList`, []);
  104. }
  105. const sizeLimit = file.size / 1024 / 1024 < 1;
  106. if (sizeLimit) return true;
  107. else {
  108. this.$message.error('文件超出10M!');
  109. return false;
  110. }
  111. },
  112. defalutProcess(val) {
  113. if (_.isArray(val)) {
  114. let newArr = val.map(item => {
  115. let object = {};
  116. object.name = item.name;
  117. object.url = item.url;
  118. return object;
  119. });
  120. this.$set(this, `fileList`, newArr);
  121. } else if (_.isObject(val)) {
  122. let object = {};
  123. if (_.get(val, `url`)) {
  124. object.name = val.name;
  125. object.url = val.url;
  126. this.$set(this, `fileList`, [object]);
  127. }
  128. } else if (typeof val === 'string') {
  129. this.$set(this, `fileList`, [{ name: '附件', url: val }]);
  130. }
  131. },
  132. },
  133. };
  134. </script>
  135. <style lang="less" scoped></style>