多媒体信息发布平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
7.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. <template>
  2. <div class="components_upload">
  3. <!-- 上传文件 -->
  4. <div class="">
  5. <el-button class="cont_upload_btn" round type="primary">上传</el-button>
  6. <input id="upFile" type="file" name="upFile" @change="changeFile($event)" />
  7. <!-- <img v-if="coverUrl" :src="coverUrl" alt="封面" /> -->
  8. <!-- <el-progress :percentage="filePercent" />
  9. {{ filePercent }} -->
  10. </div>
  11. <!-- 上传封面 -->
  12. <el-upload action="#" list-type="picture-card" :auto-upload="false">
  13. <!-- <i slot="default" class="el-icon-plus"></i> -->
  14. <img src="@/assets/images/t-sc.png" alt />
  15. <div slot="file" slot-scope="{file}">
  16. <img class="el-upload-list__item-thumbnail" :src="file.url" alt />
  17. </div>
  18. </el-upload>
  19. <!-- 上传列表layer -->
  20. <div class="upload_layer">
  21. <!-- width="736px" -->
  22. <el-dialog
  23. title="上传列表"
  24. :close-on-click-modal="false"
  25. :show-close="false"
  26. :visible.sync="uploadListVisible"
  27. height="384px"
  28. >
  29. <!-- :status="percentage === 100 ? undefined : 'success'" -->
  30. <el-table :data="fileData" :header-cell-style="{ color: '#333' }">
  31. <el-table-column align="center" prop="fileNames" label="文件名" />
  32. <el-table-column align="center" prop="formatType" label="类型" />
  33. <el-table-column align="center" prop="fileSize" label="大小" />
  34. <el-table-column align="center" prop="filePercentTxt" label="状态">
  35. <template>
  36. <div class="loadingModal" :style="{ 'height': '100%' }">
  37. <!-- :format="format" -->
  38. <!-- <el-progress
  39. :stroke-width="6"
  40. :percentage="filePercent"
  41. :color="colors"
  42. /> -->
  43. {{ filePercentTxt }}
  44. </div>
  45. </template>
  46. </el-table-column>
  47. <el-table-column align="center" prop="handle" label="操作">
  48. <template slot-scope="scope">
  49. <el-button
  50. type="primary"
  51. round
  52. class="on_off_btn"
  53. @click="end(scope.$index, scope.row)"
  54. >{{ loading_txt }}</el-button>
  55. <el-button
  56. type="info"
  57. round
  58. class="upload_delt"
  59. @click="handleRecord(scope.$index, scope.row)"
  60. >删除</el-button>
  61. </template>
  62. </el-table-column>
  63. </el-table>
  64. <div class="upload_list_right">
  65. <!-- <el-upload
  66. class="upload-demo"
  67. action="https://jsonplaceholder.typicode.com/posts/"
  68. multiple
  69. :limit="3"
  70. >
  71. <div class="right_upload">点击上传</div>
  72. </el-upload> -->
  73. <input type="button" name="开始上传" value="开始上传" @click="uploadFile()" />
  74. <div class="upload_return" @click="uploadListVisible=false">返回</div>
  75. </div>
  76. </el-dialog>
  77. </div>
  78. </div>
  79. </template>
  80. <script>
  81. import * as qiniu from 'qiniu-js'
  82. import { getQiniuToken } from '@/api/material/material'
  83. export default {
  84. name: 'Qiniu',
  85. data() {
  86. return {
  87. file: null,
  88. token: '',
  89. baseurl: 'qiniu.aiyxlib.com',
  90. fileData: [],
  91. uploadFileUrl: null,
  92. filePercent: 0,
  93. fileNames: '',
  94. formatType: '',
  95. postfix: '',
  96. fileSize: '',
  97. uploadListVisible: false,
  98. loading_txt: '暂停',
  99. colors: '#1e9f4c',
  100. uploadListData: [],
  101. filePercentTxt: '上传中'
  102. }
  103. },
  104. mounted() {
  105. this.getQiniuToken()
  106. },
  107. methods: {
  108. changeFile(e) {
  109. // 获取文件
  110. this.file = e.target.files[0]
  111. this.formatType = this.file.type.substring(0, this.file.type.indexOf('/'))
  112. console.log('formatType', this.formatType)
  113. this.fileNames = this.file.name
  114. console.log('fileNames', this.fileNames)
  115. this.postfix = this.fileNames.substring(
  116. this.fileNames.lastIndexOf('.') + 1,
  117. this.fileNames.length
  118. )
  119. console.log(this.postfix)
  120. this.fileSize = this.file.size
  121. console.log('fileSize', this.fileSize)
  122. if (this.formatType === 'image') {
  123. const isJPG = this.file.type === 'image/jpeg' || this.file.type === 'image/png' || this.file.type === 'image/bmp' || this.file.type === 'image/gif'
  124. const isLt2M = this.file.size / 1024 / 1024 < 4
  125. if (!isJPG) {
  126. this.$message.error('图片只支持bmp、jpg、png、gif格式的文件')
  127. }
  128. if (!isLt2M) {
  129. this.$message.error('图片大小不能超过 4MB !')
  130. }
  131. }
  132. // this.uploadListVisible = true
  133. // this.fileData.push({
  134. // fileNames: this.fileNames,
  135. // formatType: this.formatType,
  136. // fileSize: this.fileSize,
  137. // filePercentTxt: this.filePercentTxt
  138. // })
  139. // console.log(this.fileData[0].filePercent)
  140. },
  141. getQiniuToken() {
  142. getQiniuToken().then(res => {
  143. this.token = res.data
  144. })
  145. },
  146. // format(percentage) {
  147. // return percentage === 100 ? '100%' : `${percentage}%`
  148. // },
  149. uploadFile() {
  150. // 当前VUE中的this 是指向实例,相当于父级,指向指不到子级中。所需需要一个变量 _this 存储this得指向。
  151. const _this = this
  152. // 获取身份的token
  153. const token = _this.token
  154. console.log(token)
  155. // 上传时的配置
  156. var config = {
  157. useCdnDomain: true
  158. }
  159. // 设置文件的配置
  160. var putExtra = {
  161. fname: '',
  162. params: {},
  163. mimeType: null
  164. }
  165. // 实例化七牛云的上传实例
  166. var observable = qiniu.upload(_this.file, _this.file.name, token, putExtra, { resumingByFileSize: 4 * 1024 * 1024 }, config)
  167. // 设置实例的监听对象
  168. var observer = {
  169. // 接收上传进度信息
  170. next(res) {
  171. // 上传进度
  172. console.log(res)
  173. console.log(res.total.size)
  174. _this.filePercent = parseInt(res.total.percent)
  175. console.log(_this.filePercent)
  176. this.filePercentTxt = '上传中'
  177. if (_this.filePercent == 100) {
  178. // _this.fileData.push(_this.filePercent)
  179. // _this.fileData.forEach((value, index) => {
  180. // value['filePercent'] = parseInt(res.total.percent)
  181. // })
  182. this.filePercentTxt = '上传成功'
  183. console.log('success')
  184. }
  185. console.log(_this.fileData)
  186. // _this.fileData[0].filePercent = parseInt(res.total.percent)
  187. // console.log(_this.fileData[0].filePercent)
  188. // _this.fileData.forEach((value, index) => {
  189. // value['filePercent'] = parseInt(res.total.percent)
  190. // })
  191. },
  192. // 接收上传错误信息
  193. error(err) {
  194. console.log(err)
  195. },
  196. // 接收上传完成后的信息
  197. complete(res) {
  198. this.filePercentTxt = '上传成功'
  199. console.log(res)
  200. console.log(res.key)
  201. _this.uploadFileUrl = 'http://' + _this.baseurl + '/' + res.key
  202. console.log(_this.uploadFileUrl)
  203. // console.log(_this.fileUrl.push({ name: _this.file.name, url: _this.fileUrl }))
  204. // console.log(_this.fileUrl)
  205. }
  206. }
  207. // 上传开始
  208. var subscription = observable.subscribe(observer)
  209. // subscription.unsubscribe() 停止当前文件上传
  210. }
  211. }
  212. }
  213. </script>
  214. <style lang="scss" scoped>
  215. .components_upload{
  216. position: relative;
  217. // width: 100%;
  218. }
  219. #upFile{
  220. display: block;
  221. width: 114px;
  222. height: 34px;
  223. padding: 0;
  224. font-size: 14px;
  225. border: none;
  226. opacity: 0;
  227. }
  228. .cont_upload_btn{
  229. position: absolute;
  230. left: 0;
  231. top: 0;
  232. }
  233. </style>