filepond-upload.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <template>
  2. <div>
  3. <file-pond
  4. ref="pond"
  5. name="file"
  6. label-idle="选择视频或者拖动视频到此处"
  7. label-file-processing="视频正在上传,请稍后"
  8. label-file-processing-aborted="视频上传被取消"
  9. label-tap-to-retry="尝试重试"
  10. label-file-processing-complete="视频上传成功!"
  11. accepted-file-types="video/*, .flv, .mkv"
  12. :allow-multiple="false"
  13. :instant-upload="true"
  14. :server="server"
  15. :files="myFiles"
  16. @init="handleFilePondInit"
  17. @processfile="success"
  18. />
  19. </div>
  20. </template>
  21. <script>
  22. // Import Vue FilePond
  23. import vueFilePond from 'vue-filepond'
  24. // Import FilePond styles
  25. import 'filepond/dist/filepond.min.css'
  26. // Import FilePond plugins
  27. // Please note that you need to install these plugins separately
  28. // Import image preview plugin styles
  29. import 'filepond-plugin-image-preview/dist/filepond-plugin-image-preview.min.css'
  30. // Import image preview and file type validation plugins
  31. import FilePondPluginFileValidateType from 'filepond-plugin-file-validate-type'
  32. import FilePondPluginImagePreview from 'filepond-plugin-image-preview'
  33. import CryptoJS from 'crypto-js'
  34. // Create component
  35. const FilePond = vueFilePond(FilePondPluginFileValidateType, FilePondPluginImagePreview)
  36. let videMessage = {}
  37. export default {
  38. name: 'FilePondUpdate',
  39. components: {
  40. FilePond
  41. },
  42. videMessage,
  43. data() {
  44. return {
  45. videMessage: {},
  46. myFiles: [],
  47. server: {
  48. url: '/api/file/upload/video',
  49. process: {
  50. headers: {
  51. 'X-XSRF-TOKEN': this.$cookies.get('XSRF-TOKEN')
  52. },
  53. ondata: (formData) => {
  54. // TODO 根据登录状态获取 userId
  55. formData.append('userId', 1101269176320)
  56. return formData
  57. },
  58. onload(res) {
  59. var resp = JSON.parse(res)
  60. // 返回上传数据
  61. videMessage = resp
  62. }
  63. }
  64. }
  65. }
  66. },
  67. created() {
  68. },
  69. methods: {
  70. handleFilePondInit() {
  71. console.log('FilePond has initialized')
  72. // FilePond instance methods are available on `this.$refs.pond`
  73. },
  74. success() {
  75. this.$emit('video', videMessage)
  76. },
  77. cancelUpload() {
  78. console.log('取消上传的文件')
  79. },
  80. // TODO 计算文件的 sha256 值,然后传递给后端做判断后再确定是否需要上传文件
  81. uploadCrt(param) {
  82. var contractFile = param.file
  83. var reader = new FileReader(); var self = this
  84. var blobSlice = File.prototype.mozSlice || File.prototype.webkitSlice || File.prototype.slice
  85. var chunkSize = 6 * 1024 * 1024
  86. var chunks = Math.ceil(contractFile.size / chunkSize)
  87. var currentChunk = 0
  88. var hasher = CryptoJS.algo.SHA256.create()
  89. var start = currentChunk * chunkSize
  90. var end = start + chunkSize >= contractFile.size ? contractFile.size : start + chunkSize
  91. reader.readAsArrayBuffer(blobSlice.call(contractFile, start, end))
  92. reader.onload = function(evt) {
  93. var fileStr = evt.target.result
  94. var tmpWordArray = self.arrayBufferToWordArray(fileStr)
  95. hasher.update(tmpWordArray)
  96. currentChunk += 1
  97. fileStr = null
  98. tmpWordArray = null
  99. if (currentChunk < chunks) {
  100. var start = currentChunk * chunkSize
  101. var end = start + chunkSize >= contractFile.size ? contractFile.size : start + chunkSize
  102. reader.readAsArrayBuffer(blobSlice.call(contractFile, start, end))
  103. }
  104. }
  105. reader.onloadend = function() {
  106. contractFile = null
  107. var hash = hasher.finalize()
  108. hash.toString()// 计算结果
  109. hasher = null
  110. blobSlice = null
  111. reader = null
  112. hash = null
  113. }
  114. },
  115. arrayBufferToWordArray(ab) {
  116. var i8a = new Uint8Array(ab)
  117. var a = []
  118. for (var i = 0; i < i8a.length; i += 4) {
  119. a.push(i8a[i] << 24 | i8a[i + 1] << 16 | i8a[i + 2] << 8 | i8a[i + 3])
  120. }
  121. return CryptoJS.lib.WordArray.create(a, i8a.length)
  122. }
  123. }
  124. }
  125. </script>