hw_obs_upload.vue 5.7 KB

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