博客列表 >uniapp原生视频音乐图片文件上传

uniapp原生视频音乐图片文件上传

Time
Time原创
2022年10月12日 17:08:43629浏览
  1. <template>
  2. <view>
  3. <view class="img">
  4. <view>
  5. <progress :percent="percent" stroke-width="10"></progress>
  6. </view>
  7. <view>
  8. <button type="primary" :loading="loading" :disabled="disabled"
  9. @click="upload(0)">选择照片(请选择png、jpg、gif)</button>
  10. <img :src="img_src" alt="">
  11. </view>
  12. </view>
  13. <view class="video">
  14. <button type="primary" @tap="upload(1)">选择视频(请选择mp4格式)</button>
  15. <video :src="src"></video>
  16. </view>
  17. <view class="mp3">
  18. <button type="primary" @tap="upload(2)">选择音乐(请选择mp3格式)</button>
  19. <audio :src="audio_src" controls :name="name" author="暂无" :poster='poster'>
  20. 不支持
  21. </audio>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. import jsmediatags from '@/static/jsmediatags.min.js';
  27. var _self;
  28. export default {
  29. data() {
  30. return {
  31. src: '',
  32. img_src: '/static/kong.jpg',
  33. audio_src: 'http://tp6.com/upload/634658bfccff2.mp3',
  34. name: '纯音乐',
  35. poster: 'https://bjetxgzv.cdn.bspapp.com/VKCEYUGU-uni-app-doc/7fbf26a0-4f4a-11eb-b680-7980c8a877b8.png',
  36. percent: 0,
  37. loading: false,
  38. disabled: false
  39. }
  40. },
  41. methods: {
  42. upload: function(e) {
  43. // this.audio_img();return;
  44. _self = this;
  45. if (e == 0) {
  46. this.chooseImages()
  47. }
  48. if (e == 1) {
  49. this.chooseVideo()
  50. }
  51. if (e == 2) {
  52. this.chooseFile()
  53. }
  54. },
  55. chooseImages() {
  56. uni.chooseImage({
  57. count: 1,
  58. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  59. sourceType: ['album'], //从相册选择
  60. success: function(res) {
  61. const tempFilePaths = res.tempFilePaths;
  62. const uploadTask = uni.uploadFile({
  63. url: 'http://tp6.com/index.php',
  64. filePath: tempFilePaths[0],
  65. name: 'file',
  66. formData: {
  67. 'user': 'test1'
  68. },
  69. success: function(uploadFileRes) {
  70. console.log(uploadFileRes.data);
  71. _self.img_src = uploadFileRes.data;
  72. }
  73. });
  74. uploadTask.onProgressUpdate(function(res) {
  75. _self.percent = res.progress;
  76. console.log('上传进度' + res.progress);
  77. console.log('已经上传的数据长度' + res.totalBytesSent);
  78. console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
  79. });
  80. },
  81. error: function(e) {
  82. console.log(e);
  83. }
  84. });
  85. },
  86. chooseVideo() {
  87. uni.chooseVideo({
  88. count: 1,
  89. maxDuration: 60,
  90. sourceType: ['camera', 'album'],
  91. success: function(res) {
  92. const tempFilePaths = res.tempFilePath;
  93. const uploadTask = uni.uploadFile({
  94. url: 'http://tp6.com/index.php',
  95. filePath: tempFilePaths,
  96. name: 'file',
  97. formData: {
  98. 'user': 'test2'
  99. },
  100. success: function(uploadFileRes) {
  101. console.log(uploadFileRes.data);
  102. _self.src = uploadFileRes.data;
  103. }
  104. });
  105. uploadTask.onProgressUpdate(function(res) {
  106. // console.log(res);
  107. // _self.src = res.progress;
  108. console.log('上传进度' + res.progress);
  109. console.log('已经上传的数据长度' + res.totalBytesSent);
  110. console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
  111. });
  112. },
  113. error: function(e) {
  114. console.log(e);
  115. }
  116. });
  117. },
  118. chooseFile() {
  119. uni.chooseFile({
  120. count: 1,
  121. type: 'mp3',
  122. maxDuration: 60,
  123. sourceType: ['camera', 'album'],
  124. success: function(res) {
  125. // console.log(res.tempFiles[0]);return;
  126. _self.audio_img(res.tempFiles[0]); //设置音乐播放器封面
  127. const tempFilePaths = res.tempFiles[0].path;
  128. _self.name = res.tempFiles[0]['name'].substring(0, res.tempFiles[0]['name']
  129. .lastIndexOf("."))
  130. const uploadTask = uni.uploadFile({
  131. url: 'http://tp6.com/index.php',
  132. filePath: tempFilePaths,
  133. name: 'file',
  134. formData: {
  135. 'user': 'test2'
  136. },
  137. success: function(uploadFileRes) {
  138. console.log(uploadFileRes.data);
  139. _self.audio_src = uploadFileRes.data;
  140. }
  141. });
  142. uploadTask.onProgressUpdate(function(res) {
  143. // _self.src = res.progress;
  144. console.log('上传进度' + res.progress);
  145. console.log('已经上传的数据长度' + res.totalBytesSent);
  146. console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
  147. });
  148. },
  149. error: function(e) {
  150. console.log(e);
  151. }
  152. });
  153. },
  154. audio_img(file) {
  155. jsmediatags.read(file, {
  156. onSuccess: function(tag) {
  157. console.log(tag);
  158. var picture = tag.tags.picture; // create reference to track art
  159. var base64String = "";
  160. for (var i = 0; i < picture.data.length; i++) {
  161. base64String += String.fromCharCode(picture.data[i]);
  162. }
  163. var imageUri = "data:" + picture.format + ";base64," + window.btoa(base64String);
  164. _self.poster = imageUri;
  165. // console.log(imageUri);
  166. // use ajax to upload tag info, or create some new form elements
  167. },
  168. onError: function(error) {
  169. console.log(error);
  170. }
  171. });
  172. return;
  173. }
  174. },
  175. onLoad: function() {
  176. }
  177. }
  178. </script>
  179. <style>
  180. img {
  181. width: 300rpx;
  182. height: 300rpx;
  183. }
  184. </style>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议