Vue怎么实现自定义视频和图片上传

其他教程   发布日期:2025年04月02日   浏览次数:193

今天小编给大家分享一下Vue怎么实现自定义视频和图片上传的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

代码如下:

  1. <template>
  2. <div class="body">
  3. <span>测试</span>
  4. <span>视频上传</span>
  5. <!-- 隐藏input框 -->
  6. <input class="input-video" multiple type="file" accept="video/*"
  7. @change="uploadVideo11($event)">
  8. <div v-if="!videoUrl" class="no-bg wh">
  9. <el-progress class="progress-video" v-if="progress" type="circle" :percentage="videoUploadPercent"></el-progress>
  10. <div v-if="!progress" @click="uploadVideo">
  11. <span>点击上传视频</span>
  12. </div>
  13. </div>
  14. <video v-else="videoUrl" class="wh" v-bind:src="videoUrl" controls="controls">
  15. 您的浏览器不支持视频播放
  16. </video>
  17. <span>-------------------------------------------------------------------------</span>
  18. <span>图片上传</span>
  19. <!-- <el-upload
  20. :disabled="disabled" 删除图片后禁止弹窗弹出
  21. :action="this.$store.state.updataurl" 图片上传路径
  22. :headers="headers" 请求头
  23. :show-file-list="false" 文件列表
  24. :on-success="handleAvatarSuccess" 上传成功回调
  25. :before-upload="beforeAvatarUpload" 上传前回调
  26. :on-remove="handleRemove" 移除回调
  27. :on-progress="picUploadProcess" 上传进度条
  28. class="wh" > -->
  29. <el-upload :disabled="disabled" :action="this.$store.state.updataurl" :headers="headers" :show-file-list="false"
  30. :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload" :on-remove="handleRemove"
  31. :on-progress="picUploadProcess" class="wh">
  32. <div v-if="cover" class="change-img wh">
  33. <!-- 地址拼接 -->
  34. <img :src="picUrl + cover" class="wh" />
  35. <div class="change">
  36. <span class="arr">更换</span>
  37. <span>&nbsp;|&nbsp;</span>
  38. <span class="arr" @click="handleRemove">删除</span>
  39. </div>
  40. </div>
  41. <div v-else class="no-bg wh">
  42. <el-progress v-if="picFlag" type="circle" :percentage="picUploadPercent"></el-progress>
  43. <div v-else @click="disabled = false">
  44. <span class="note">点击上传封面图片</span>
  45. </div>
  46. </div>
  47. </el-upload>
  48. </div>
  49. </template>
  50. <script>
  51. import axios from 'axios'
  52. export default {
  53. data() {
  54. return {
  55. // 视频
  56. videoUrl: '', //视频播放地址
  57. progress: false,
  58. videoUploadPercent: 0,
  59. // 图片
  60. picUrl: this.$store.state.baseurl, //图片前缀
  61. disabled: false, //删除图片后禁止文件选择弹窗弹出
  62. picFlag: false, //进度条显示标识
  63. picUploadPercent: 0,
  64. cover: ''
  65. }
  66. },
  67. computed: {
  68. headers() {
  69. return {
  70. Authorization: this.$store.getters.token || localStorage.getItem("token"),
  71. // soloFileName: 'video', //和后端协商的默认标识
  72. };
  73. },
  74. },
  75. methods: {
  76. // 视频上传
  77. uploadVideo() {
  78. let ipFile = document.querySelector('.input-video')
  79. ipFile.click();
  80. this.videoUploadPercent = 0
  81. },
  82. async uploadVideo11(e) {
  83. // console.log("uploadVideo11", e)
  84. // console.log(e.target.files[0])
  85. this.progress = true
  86. let file = e.target.files[0]
  87. console.log("file", file)
  88. // multipart/form-data 格式
  89. let formData = new FormData()
  90. formData.append('file', file)
  91. let config = {
  92. //添加请求头
  93. headers: {
  94. Authorization: this.$store.getters.token || localStorage.getItem("token"),
  95. "Content-Type": "multipart/form-data"
  96. },
  97. // 添加进度条
  98. onUploadProgress: (progressEvent) => {
  99. this.videoUploadPercent = Number((progressEvent.loaded / progressEvent.total * 100).toFixed(0)) * 1
  100. // console.log("this.videoUploadPercent", this.videoUploadPercent)
  101. }
  102. };
  103. axios.post(this.$store.state.videoUploadUrl, formData, config, { timeout: 1000 * 60 * 2 }) //设置超时2分钟
  104. .then(async (res) => {
  105. console.log("视频上传", res);
  106. this.videoUrl = res.data.data.playUrl //端传过来的视频播放地址
  107. if (this.videoUrl) {
  108. this.progress = false
  109. this.$message({
  110. message: '上传成功',
  111. type: 'success'
  112. });
  113. }
  114. }).catch(err => {
  115. this.progress = false
  116. console.log(err);
  117. this.$message.error({
  118. message: '上传失败'
  119. });
  120. })
  121. },
  122. // 图片上传
  123. handleAvatarSuccess: function (response, file, fileList) {
  124. if (file.response.code == 200) {
  125. this.cover = file.response.data.src; //后端传过来的图片地址
  126. this.picFlag = false
  127. this.picUploadPercent = 0
  128. }
  129. },
  130. handleRemove(file) {
  131. this.cover = ''
  132. this.disabled = true
  133. // 1.获取将要删除图片的临时路径
  134. // const filePath = file.response.data.tmp_path
  135. // // 2.从pics数组中,找到图片对应的索引值
  136. // const i = this.formData.pics.findIndex(x => x.pic === filePath)
  137. // // 3.调用splice方法,移除图片信息
  138. // this.formData.splice(i, 1)
  139. },
  140. beforeAvatarUpload(file) {
  141. const isIMG = /^image/(jpe?g|png|gif)$/.test(file.type);
  142. const isLt2M = file.size / 1024 / 1024 < 2;
  143. if (!isIMG) {
  144. this.$message.error("上传图片只能是 JPG/PNG/GIF 格式!");
  145. }
  146. if (!isLt2M) {
  147. this.$message.error("上传图片大小不能超过 2MB!");
  148. }
  149. return isIMG && isLt2M;
  150. },
  151. picUploadProcess(event, file, fileList) {
  152. this.picFlag = true;
  153. this.picUploadPercent = file.percentage.toFixed(0) * 1;
  154. },
  155. }
  156. }
  157. </script>
  158. <style lang="less" scoped>
  159. // 进度条
  160. /deep/ .el-progress-circle {
  161. height: 100px !important;
  162. width: 100px !important;
  163. }
  164. .body {
  165. display: flex;
  166. flex-direction: column;
  167. }
  168. .no-bg {
  169. display: flex;
  170. justify-content: center;
  171. align-items: center;
  172. border: 1px dashed #000000;
  173. background-color: #FBFBFC;
  174. border-radius: 5px;
  175. cursor: pointer; //变小手
  176. }
  177. .wh {
  178. width: 320px;
  179. height: 180px;
  180. border-radius: 5px;
  181. }
  182. .change-img {
  183. position: relative;
  184. .change {
  185. opacity: 0;
  186. position: absolute;
  187. bottom: 8%;
  188. left: 35%;
  189. padding: 6px 11px;
  190. background-color: rgba(0, 0, 0, 0.7);
  191. border-radius: 4px;
  192. color: #FFFFFF;
  193. }
  194. }
  195. .change-img:hover .change {
  196. opacity: 1;
  197. }
  198. .arr {
  199. cursor: default;
  200. }
  201. </style>

以上就是Vue怎么实现自定义视频和图片上传的详细内容,更多关于Vue怎么实现自定义视频和图片上传的资料请关注九品源码其它相关文章!