upload.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <div>
  3. <div class="upload">
  4. <el-button @click="dialogUploadVisible = true" v-if="target == 'submit'"> <i class="iconfont icon-a-tuiguangsucaishangchuan1" style="color:#3874F6"></i> 上传</el-button>
  5. </div>
  6. <el-button type="warning" size="small" @click="dialogUploadVisible = true" icon="el-icon-upload" v-if="target == 'archives'">上 传</el-button>
  7. <el-button round v-else-if="target == 'avatar'" @click="dialogUploadVisible = true">上传头像</el-button>
  8. <el-dialog title="文件上传" :visible.sync="dialogUploadVisible" width="500px" :close-on-click-modal="false"
  9. :before-close="clearFiles">
  10. <el-upload style="width:100%" ref="my-upload" class="upload-demo" action="#" :auto-upload="false"
  11. :show-file-list="false" :on-change="handleChange" drag multiple>
  12. <i class="el-icon-upload"></i>
  13. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  14. </el-upload>
  15. <div class="progress_panel" v-for="file in filelist" :key="file.uid">
  16. <img v-if="file.type === 'DOC' || file.type === 'DOCX'" width="30" src="@/assets/file_icons/word.png"
  17. alt="">
  18. <img v-else-if="file.type === 'PDF'" width="30" src="@/assets/file_icons/pdf.png" alt="">
  19. <img v-else-if="file.type === 'MP4' || file.type === 'AVI'" width="30" src="@/assets/file_icons/video.png"
  20. alt="">
  21. <img v-else-if="file.type === 'XLS' || file.type === 'XLSX'" width="30" src="@/assets/file_icons/excel.png"
  22. alt="">
  23. <img v-else-if="file.type === 'PNG' || file.type === 'JPG'|| file.type === 'JPEG'" width="30"
  24. src="@/assets/file_icons/image.png" alt="">
  25. <img v-else-if="file.type === 'PPT' || file.type === 'PPTX'" width="30" src="@/assets/file_icons/ppt.png"
  26. alt="">
  27. <img v-else width="30" src="@/assets/file_icons/unknow.png" alt="">
  28. <div>
  29. <p v-if="file.progress === 100" style="float:right"><span style="color:#67C23A">●</span>上传成功</p>
  30. <p>{{file.raw?file.raw.name:'暂无上传文件'}}</p>
  31. <el-progress :percentage="file.progress" :show-text="false"></el-progress>
  32. </div>
  33. </div>
  34. </el-dialog>
  35. </div>
  36. </template>
  37. <script>
  38. export default {
  39. name:'upload',
  40. props: ['folderid'],
  41. data () {
  42. return {
  43. dialogUploadVisible: false,
  44. params: {
  45. "classname": "system.attachment.huawei.OBS",
  46. "method": "getFileName",
  47. "content": {
  48. "filename": '',
  49. "filetype": '',
  50. "parentid": JSON.parse(sessionStorage.getItem('Parameter')).appfolderid//归属文件夹ID
  51. }
  52. },
  53. file: {},
  54. filelist: []
  55. }
  56. },
  57. props:{
  58. target: String,
  59. classType: {
  60. default() {
  61. return ''
  62. }
  63. }
  64. },
  65. methods: {
  66. handleChange (file, filelist) {
  67. var ext = file.name.substr(file.name.indexOf('.')+1)
  68. let type = []
  69. let result = ''
  70. if(this.classType == '1') {
  71. console.log(1111);
  72. type = ['png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'svg', 'tiff']
  73. result = type.some(item => {
  74. return item == ext
  75. })
  76. if(!result) {
  77. this.$notify({
  78. title:'提示',
  79. message:'只能上传图片',
  80. type:'warning'
  81. })
  82. this.clearFiles()
  83. return
  84. }
  85. } else if(this.classType == '2') {
  86. type = ['mp4', 'ogg', 'webm']
  87. result = type.some(item => {
  88. return item == ext
  89. })
  90. if(!result) {
  91. this.$notify({
  92. title:'提示',
  93. message:'只能上传视频',
  94. type:'warning'
  95. })
  96. this.clearFiles()
  97. return
  98. }
  99. }
  100. this.filelist = filelist
  101. var index = file.raw.name.lastIndexOf(".");
  102. this.params.content.filename = file.raw.name
  103. this.params.content.filetype = ext
  104. this.getUploadUrl(file, ext)
  105. },
  106. // 获取华为云上传地址
  107. async getUploadUrl (file, ext) {
  108. // this.params.content.parentid = 1
  109. const res = await this.$api.requested(this.params)
  110. let url = res.data.uploadurl
  111. let obsfilename = res.data.serialfilename
  112. this.upoladFileToServer(url, file, ext, obsfilename)
  113. },
  114. // 上传到华为云
  115. async upoladFileToServer (url, file, ext, obsfilename) {
  116. let THIS = this
  117. let config = {
  118. headers: ext === 'pdf' ? { 'Content-Type': 'application/pdf' } : { 'Content-Type': 'application/octet-stream' },
  119. onUploadProgress: function (progressEvent) {
  120. let percent = progressEvent.loaded / progressEvent.total * 100
  121. THIS.filelist.forEach(e => {
  122. if (e.uid === file.uid) {
  123. THIS.$set(e, 'type', ext.toUpperCase());
  124. THIS.$set(e, 'progress', percent);
  125. }
  126. })
  127. },
  128. }
  129. const res = await this.$upload.hw_upload(url, file.raw, config)
  130. this.createFileRecord(obsfilename)
  131. },
  132. // 上传成功以后生成附件记录
  133. async createFileRecord (obsfilename) {
  134. let param = {
  135. "classname": "system.attachment.huawei.OBS",
  136. "method": "uploadSuccess",
  137. "content": {
  138. "serialfilename": obsfilename
  139. }
  140. }
  141. this.$api.requested(param).then( res => {
  142. this.dialogUploadVisible = false
  143. this.clearFiles()
  144. this.$emit('onSuccess',res.data.attachmentids[0])
  145. })
  146. },
  147. clearFiles () {
  148. this.$refs['my-upload'].clearFiles()
  149. this.filelist = []
  150. this.dialogUploadVisible = false
  151. }
  152. }
  153. }
  154. </script>
  155. <style>
  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. .upload .el-button {
  183. width: 100%;
  184. }
  185. </style>