preview_upload.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <div>
  3. <div v-if="type === 'button'" class="picture_list">
  4. <div class="flex-align-center flex-between pionter" v-for="(img,index) in fileLinkList" :key="img.index">
  5. <div class="flex-align-center">
  6. <img width="30" :src="img.type.split('/')[0] === 'image'?img.url:require('@/assets/file_icons/file.svg')" class="inline-16" alt="">
  7. <div class="file__link inline-16">
  8. <p>{{img.name}}</p>
  9. </div>
  10. </div>
  11. <i style="color:red;" class="el-icon-delete" @click="handleRemove(index)"></i>
  12. </div>
  13. <el-upload
  14. action="#"
  15. :on-change="onChange"
  16. :show-file-list="false"
  17. :auto-upload="false"
  18. multiple>
  19. <el-button size="small" type="text" icon="el-icon-paperclip">点击上传</el-button>
  20. </el-upload>
  21. </div>
  22. <div v-else class="flex-align-stretch">
  23. <div v-for="(img,index) in fileLinkList" :key="index" class="upload_image__panel">
  24. <img :src="img.type.split('/')[0] === 'image'?img.url:require('@/assets/file_icons/file.svg')" alt="">
  25. <div class="upload_image__panel_mod flex-align-center flex-around">
  26. <i class="el-icon-delete" style="font-size:16px" @click="handleRemove(index)"></i>
  27. </div>
  28. </div>
  29. <el-upload
  30. :accept="accept"
  31. action="#"
  32. :on-change="onChange"
  33. :show-file-list="false"
  34. :auto-upload="false"
  35. multiple>
  36. <i class="el-icon-plus"></i>
  37. </el-upload>
  38. </div>
  39. </div>
  40. </template>
  41. <script>
  42. export default {
  43. props:['folderid','bindData','type','accept'],
  44. data() {
  45. return {
  46. imageUrl:'',
  47. fileList:[],
  48. fileLinkList:[],
  49. params: {
  50. "classname": "system.attachment.huawei.OBS",
  51. "method": "getFileName",
  52. "content": {
  53. "filename": '',
  54. "filetype": '',
  55. "parentid": ""//归属文件夹ID
  56. }
  57. },
  58. };
  59. },
  60. methods: {
  61. onChange (file,filelist) {
  62. this.fileList = filelist
  63. this.beforeUpload(file.raw)
  64. },
  65. beforeUpload (file) {
  66. console.log(file)
  67. var that = this
  68. var fileReader = new FileReader();
  69. fileReader.readAsDataURL(file);
  70. fileReader.onload = function(e) {
  71. var result = e.target.result;
  72. that.fileLinkList.push({url:result,type:file.type,name:file.name})
  73. }
  74. },
  75. handleRemove (index) {
  76. this.fileList = this.fileList.filter((e,idx)=>{
  77. if (idx !== index) {
  78. return e
  79. }
  80. })
  81. this.fileLinkList = this.fileLinkList.filter((e,idx)=>{
  82. if (idx !== index) {
  83. return e
  84. }
  85. })
  86. },
  87. toUpload () {
  88. this.fileList.forEach(file=>{
  89. let index = file.name.lastIndexOf(".");
  90. let ext = file.name.substr(index + 1);
  91. this.params.content.filename = file.name
  92. this.params.content.filetype = ext
  93. this.getUploadUrl(file, ext)
  94. })
  95. },
  96. // 获取华为云上传地址
  97. async getUploadUrl (file, ext) {
  98. this.params.content.parentid = this.folderid
  99. const res = await this.$api.requested(this.params)
  100. let url = res.data.uploadurl
  101. let obsfilename = res.data.serialfilename
  102. this.upoladFileToServer(url, file, ext, obsfilename)
  103. },
  104. // 上传到华为云
  105. async upoladFileToServer (url, file, ext, obsfilename) {
  106. let THIS = this
  107. let config = {
  108. headers: ext === 'pdf' ? { 'Content-Type': 'application/pdf' } : ext === 'svg'?{ 'Content-Type': 'image/svg+xml' } : { 'Content-Type': 'application/octet-stream' },
  109. }
  110. const res = await this.$upload.hw_upload(url, file.raw, config)
  111. this.createFileRecord(obsfilename)
  112. },
  113. // 上传成功以后生成附件记录
  114. async createFileRecord (obsfilename,attinfos) {
  115. let obj = {
  116. "serialfilename": obsfilename
  117. }
  118. obj = Object.assign({},obj,this.bindData)
  119. let param = {
  120. "classname": "system.attachment.huawei.OBS",
  121. "method": "uploadSuccess",
  122. "content":obj
  123. }
  124. const res = await this.$api.requested(param)
  125. if (res.code === 1) {
  126. this.$emit('onSuccess',res)
  127. this.fileList = []
  128. this.fileLinkList = []
  129. }
  130. },
  131. }
  132. }
  133. </script>
  134. <style>
  135. </style>
  136. <style scoped>
  137. .upload_image__panel{
  138. position: relative;
  139. height:148px;
  140. width: 148px;
  141. border-radius: 6px;
  142. margin-right: 10px;
  143. margin-bottom:10px;
  144. border: 1px solid #c0ccda;
  145. overflow: hidden;
  146. }
  147. .upload_image__panel img{
  148. width: 100%;
  149. height: 100%;
  150. }
  151. .upload_image__panel_mod{
  152. position: absolute;
  153. top:0;
  154. left: 0;
  155. width: 100%;
  156. height: 100%;
  157. color:#fff;
  158. background: rgba(0,0,0,.8);
  159. opacity: 0;
  160. transition: .3s linear;
  161. cursor: pointer;
  162. }
  163. .upload_image__panel:hover .upload_image__panel_mod{
  164. opacity: 1;
  165. }
  166. .pionter{
  167. margin:6px 0;
  168. padding: 10px;
  169. transition: .2s linear;
  170. cursor: pointer;
  171. border-radius: 5px;
  172. }
  173. .pionter:hover{
  174. box-shadow: 0 5px 10px rgb(0 0 0 / 10%);
  175. }
  176. </style>