FileUpload.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <el-upload
  3. drag
  4. class="upload-file"
  5. :action="uploadUrl"
  6. :on-success="handleUploadSuccess"
  7. multiple
  8. :limit="countLimit"
  9. :on-exceed="handleExceed"
  10. :before-upload="beforeFileUpload"
  11. :on-remove="handleFileRemove"
  12. :file-list="fileList"
  13. :on-error="fileUploadError"
  14. :disabled="disabled"
  15. >
  16. <i class="el-icon-upload"></i>
  17. <div class="el-upload__text">
  18. 将文件拖到此处,或
  19. <em>点击上传</em>
  20. </div>
  21. </el-upload>
  22. </template>
  23. <script>
  24. import Apis from '@/js/api.js'
  25. export default {
  26. name: 'FileUpload',
  27. data: function () {
  28. return {
  29. fileList: [],
  30. uploadUrl: process.env.API_ROOT + Apis.FILE.UPLOAD_TEST_CASE_FILE
  31. }
  32. },
  33. props: {
  34. countLimit: {
  35. type: Number,
  36. default: 1
  37. },
  38. files: Array,
  39. disabled: {
  40. type: Boolean,
  41. default: false
  42. }
  43. },
  44. watch: {
  45. files: {
  46. immediate: true,
  47. handler: function () {
  48. if (this.files.length !== this.fileList.length) {
  49. this.fileList = []
  50. this.files.map(file => {
  51. let fileName = file.substring(file.lastIndexOf('/') + 1)
  52. fileName = fileName.substring(0, fileName.lastIndexOf('_', fileName.lastIndexOf('_') - 1)) + fileName.substring(fileName.lastIndexOf('.'))
  53. this.fileList.push({ name: fileName, url: file })
  54. })
  55. }
  56. }
  57. }
  58. },
  59. methods: {
  60. handleExceed (files, fileList) {
  61. this.$message.warning(
  62. `当前限制选择 ${this.countLimit} 个文件,本次选择了 ${
  63. files.length
  64. } 个文件,共选择了 ${files.length + fileList.length} 个文件`
  65. )
  66. },
  67. beforeFileUpload (file) {
  68. console.log(file)
  69. const isPDF = file.type === 'application/pdf'
  70. const isDOC = file.type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
  71. const isEXCEL = file.type === 'application/vnd.ms-excel'
  72. const isXLS = file.type === 'application/x-xls'
  73. const isTXT = file.type === 'text/plain'
  74. const isXLSX = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  75. if (!(isDOC || isEXCEL || isPDF || isTXT || isXLS || isXLSX)) {
  76. this.$message.error('上传文件只能是 PDF 、 DOC 、DOCX 、XLS、TXT、XLSX 格式!')
  77. }
  78. return isDOC || isEXCEL || isPDF || isTXT || isXLS || isXLSX
  79. },
  80. handleUploadSuccess (response, file, fileList) {
  81. // 这两个的顺序不能换
  82. this.fileList.push(file)
  83. this.files.push(response)
  84. },
  85. fileUploadError (err, file, fileList) { // 文件上传失败调用
  86. console.log(err)
  87. this.$message.error('上传文件失败!')
  88. },
  89. handleFileRemove (file) {
  90. console.log(this.fileList)
  91. for (const i in this.fileList) {
  92. if (this.fileList[i].uid === file.uid) {
  93. // 这两个的顺序不能换
  94. this.fileList.splice(i, 1)
  95. this.files.splice(i, 1)
  96. }
  97. }
  98. console.log(this.files)
  99. }
  100. }
  101. }
  102. </script>
  103. <style scoped>
  104. .upload-file {
  105. width: 400px;
  106. }
  107. </style>