importFile.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <div>
  3. <!-- 按钮类型 -->
  4. <el-button type="success" size="mini" @click="dialogUploadVisible = true" icon="el-icon-upload">导 入</el-button>
  5. <el-dialog title="文件上传" class="import-panel" :visible.sync="dialogUploadVisible" width="500px" append-to-body :close-on-click-modal="false" :show-close="false" :before-close="clearFiles">
  6. <div slot="title"></div>
  7. <div style="padding:20px">
  8. <el-upload
  9. style="width:100%"
  10. ref="my-upload"
  11. class="upload-demo normal-margin"
  12. :accept="accept"
  13. action="#"
  14. :auto-upload="false"
  15. :show-file-list="false"
  16. :on-change="handleChange"
  17. drag
  18. multiple>
  19. <i class="el-icon-upload"></i>
  20. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  21. </el-upload>
  22. <div class="progress_panel" v-for="file in filelist" :key="file.uid">
  23. <img v-if="file.type === 'DOC' || file.type === 'DOCX'" width="30" src="../../../../../../assets/file_icons/word.png"
  24. alt="">
  25. <img v-else-if="file.type === 'PDF'" width="30" src="../../../../../../assets/file_icons/PDF.png" alt="">
  26. <img v-else-if="file.type === 'MP4' || file.type === 'AVI'" width="30" src="../../../../../../assets/file_icons/video.png"
  27. alt="">
  28. <img v-else-if="file.type === 'XLS' || file.type === 'XLSX'" width="30" src="../../../../../../assets/file_icons/excel.png"
  29. alt="">
  30. <img v-else-if="file.type === 'PNG' || file.type === 'JPG'|| file.type === 'JPEG'" width="30"
  31. src="../../../../../../assets/file_icons/image.png" alt="">
  32. <img v-else-if="file.type === 'PPT' || file.type === 'PPTX'" width="30" src="../../../../../../assets/file_icons/PPT.png"
  33. alt="">
  34. <img v-else width="30" src="../../../../../../assets/file_icons/unknow.png" alt="">
  35. <div>
  36. <p v-if="file.progress === 100" style="float:right"><span style="color:#67C23A">●</span>上传成功</p>
  37. <p>{{file.raw?file.raw.name:'暂无上传文件'}}</p>
  38. <el-progress :percentage="file.progress" :show-text="false"></el-progress>
  39. </div>
  40. </div>
  41. <slot name="errorFile"></slot>
  42. <p class="tips">• 为保证数据导入顺利,推荐您下载并使用<a :href="modelurl">《Excel标准模板》</a></p>
  43. <p class="tips">• 文件中数据不能超过5000行</p>
  44. <div class="dialog-footer">
  45. <el-button size="small" @click="dialogUploadVisible = false" class="normal-btn-width">取 消</el-button>
  46. <el-button size="small" type="warning" @click="dialogUploadVisible = false" class="normal-btn-width btn-warning">确 定</el-button>
  47. </div>
  48. </div>
  49. </el-dialog>
  50. </div>
  51. </template>
  52. <script>
  53. export default {
  54. /*
  55. folderid:文件夹id; 必填
  56. btntype:展示上传按钮的类型;
  57. accept:限制上传文件类型;
  58. bindData:附件上传成功后对应需要绑定的数据信息
  59. */
  60. props:['folderid','btntype','accept','bindData'],
  61. data () {
  62. return {
  63. dialogUploadVisible: false,
  64. params: {
  65. "classname": "system.attachment.huawei.OBS",
  66. "method": "getFileName",
  67. "content": {
  68. "filename": '',
  69. "filetype": '',
  70. "parentid": ""//归属文件夹ID
  71. }
  72. },
  73. file: {},
  74. filelist: [],
  75. CampaignList:[],
  76. activeName:'first',
  77. modelurl:'',
  78. campaignid:''
  79. }
  80. },
  81. mounted () {
  82. this.getModelUrl()
  83. },
  84. methods: {
  85. handleChange (file, filelist) {
  86. this.filelist = filelist
  87. var index = file.raw.name.lastIndexOf(".");
  88. var ext = file.name.substr(index + 1);
  89. this.params.content.filename = file.raw.name
  90. this.params.content.filetype = ext
  91. this.getUploadUrl(file, ext)
  92. },
  93. handleClick () {
  94. this.getModelUrl()
  95. },
  96. // 获取导入模板
  97. async getModelUrl () {
  98. const res = await this.$api.requested({
  99. "id": 20220913093102,
  100. "content": {},
  101. })
  102. this.modelurl = res.data
  103. },
  104. // 获取华为云上传地址
  105. async getUploadUrl (file, ext) {
  106. this.params.content.parentid = this.folderid
  107. const res = await this.$api.requested(this.params)
  108. let url = res.data.uploadurl
  109. let obsfilename = res.data.serialfilename
  110. this.upoladFileToServer(url, file, ext, obsfilename)
  111. },
  112. // 上传到华为云
  113. async upoladFileToServer (url, file, ext, obsfilename) {
  114. let THIS = this
  115. let config = {
  116. headers: ext === 'pdf' ? { 'Content-Type': 'application/pdf' } : { 'Content-Type': 'application/octet-stream' },
  117. onUploadProgress: function (progressEvent) {
  118. let percent = progressEvent.loaded / progressEvent.total * 100
  119. THIS.filelist.forEach(e => {
  120. if (e.uid === file.uid) {
  121. THIS.$set(e, 'type', ext.toUpperCase());
  122. THIS.$set(e, 'progress', percent);
  123. }
  124. })
  125. },
  126. }
  127. const res = await this.$upload.hw_upload(url, file.raw, config)
  128. this.createFileRecord(obsfilename)
  129. },
  130. // 上传成功以后生成附件记录
  131. async createFileRecord (obsfilename) {
  132. let obj = {
  133. "serialfilename": obsfilename
  134. }
  135. obj = Object.assign({},obj,this.bindData)
  136. let param = {
  137. "classname": "system.attachment.huawei.OBS",
  138. "method": "uploadSuccess",
  139. "content":obj
  140. }
  141. const res = await this.$api.requested(param)
  142. this.$emit('onImportSuccess',res)
  143. },
  144. clearFiles () {
  145. this.$refs['my-upload'].clearFiles()
  146. this.filelist = []
  147. this.dialogUploadVisible = false
  148. },
  149. }
  150. }
  151. </script>
  152. <style>
  153. .import-panel .el-dialog__header,.import-panel .el-dialog__body{
  154. padding: 0 !important;
  155. }
  156. .upload-demo > div {
  157. width: 100% !important;
  158. }
  159. .upload-demo .el-upload-dragger {
  160. width: 100% !important;
  161. }
  162. </style>
  163. <style scoped>
  164. .progress_panel {
  165. display: flex;
  166. align-items: center;
  167. padding: 10px;
  168. margin: 10px 0;
  169. border-radius: 5px;
  170. transition: linear 0.2s all;
  171. }
  172. .progress_panel:hover {
  173. box-shadow: 0px 0px 5px #ccc;
  174. }
  175. .progress_panel > div {
  176. flex: 1;
  177. padding: 0 10px;
  178. }
  179. .progress_panel > div > p {
  180. line-height: 30px;
  181. }
  182. .tips{
  183. line-height: 30px;
  184. }
  185. </style>