多媒体信息发布平台
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.

246 lines
7.8 KiB

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