notificaUpload.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <div>
  3. <el-upload
  4. :multiple="true"
  5. action="null"
  6. :file-list="fileList"
  7. :show-file-list="true"
  8. :on-preview="handlePreview"
  9. :on-remove="handleRemove"
  10. :on-success="handleSuccess"
  11. class="upload-demo"
  12. drag
  13. :limit="1"
  14. :http-request="uploadResourceRequest"
  15. >
  16. <i class="el-icon-upload" />
  17. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  18. </el-upload>
  19. </div>
  20. </template>
  21. <script>
  22. import { uploadOn, del } from './notificaUpload'
  23. import baseUrl from '../../utils/baseUrl.js'
  24. export default {
  25. name: 'FileUpload',
  26. // 接受父组件的值
  27. // props: {
  28. // value: { type: String },
  29. // code: {
  30. // type: String,
  31. // default: 'AsApplication'
  32. // }
  33. // },
  34. data() {
  35. return {
  36. processurl: process.env.NODE_ENV === "production" ? baseUrl.ROOT : baseUrl.ROOT,
  37. fileList: []
  38. }
  39. },
  40. watch: {
  41. // value(newValue, oldValue) {
  42. // console.log(newValue, 'valStr')
  43. // if (newValue) {
  44. // this.fileList = []
  45. // // 如果多个文件 process.env.VUE_APP_BASE_IMS_EXT + 'f-center/hadoop/hdfs/download?path=' +
  46. // const files = newValue.split(',')
  47. // for (let i = 0; i < files.length; i++) {
  48. // const s = files[i].substring(files[i].lastIndexOf('/') + 1, files[i].length)
  49. // const datas = {
  50. // name: s,
  51. // url: files[i]
  52. // }
  53. // this.fileList.push(datas)
  54. // }
  55. // }
  56. // }
  57. },
  58. created() {
  59. },
  60. methods: {
  61. /**
  62. * 自定义上传
  63. * @param elementUI 自动传的参数,这个参数就是文件对象
  64. */
  65. uploadResourceRequest(param) {
  66. param.status = 'uploading' // 文件上传状态
  67. param.percentage = 0 // 文件上传进度,这里设为0
  68. param.name = param.file.name // 文件名
  69. const uploadEvent = (progressEvent) => {
  70. this.progressPercent = Number(
  71. ((progressEvent.loaded / progressEvent.total) * 100).toFixed(2)
  72. )
  73. }
  74. debugger
  75. uploadOn('AsApplication', 'xxxt/image', '', param.file, 'yysd', uploadEvent).then(response => {
  76. // 请求成功
  77. const datas = {
  78. name: response.data.docFileName,
  79. url: this.processurl + 'f-center/hadoop/hdfs/download?path=' + response.data.docPath,
  80. path: response.data.docPath
  81. }
  82. this.fileList.push(datas)
  83. // 每次上传成功后就将 fileList 拼接返回给父组件
  84. let fileStr = ''
  85. const fileLists = this.fileList
  86. if (fileLists.length > 0) {
  87. for (let v = 0; v < fileLists.length; v++) {
  88. fileStr = fileLists[v].path + ','
  89. }
  90. }
  91. fileStr = fileStr.substring(0, fileStr.lastIndexOf(','))
  92. this.$emit('input', fileStr)
  93. param.status = 'success' // 上传成功后将status属性改成success
  94. }).catch(function(error) {
  95. // 请求失败处理
  96. console.log(error)
  97. param.status = 'failed'
  98. param.message = '上传失败'
  99. })
  100. },
  101. // 文件移除
  102. handleRemove(file) {
  103. const name = this.fileList.findIndex(item => {
  104. if (item.name == file.name) {
  105. return true
  106. }
  107. })
  108. console.log(file.url, '删除文件')
  109. // 调用删除接口
  110. del(file.url).then(response => {
  111. this.fileList.splice(name, 1)
  112. // 移除后重新给父组件赋值
  113. // 每次上传成功后就将 fileList 拼接返回给父组件
  114. let fileStr = ''
  115. const fileLists = this.fileList
  116. if (fileLists.length > 0) {
  117. for (let v = 0; v < fileLists.length; v++) {
  118. fileStr = fileLists[v].path + ','
  119. }
  120. }
  121. fileStr = fileStr.substring(0, fileStr.lastIndexOf(','))
  122. this.$emit('input', fileStr)
  123. })
  124. },
  125. // 文件预览
  126. handlePreview(file) {
  127. // 下载文件 或者在线预览
  128. },
  129. handleSuccess(response) {
  130. }
  131. }
  132. }
  133. </script>
  134. <style lang="scss" scoped>
  135. .editor-slide-upload {
  136. margin-bottom: 20px;
  137. ::v-deep .el-upload--picture-card {
  138. width: 20%;
  139. }
  140. }
  141. </style>