当前位置:   article > 正文

simple-uploader/vue-uploader分片上传视频——vue3_vue3 分片上传视频

vue3 分片上传视频

一、template标签内容

  1. <uploader
  2. style="margin-left: 10px"
  3. :options="optionsV.optionsV"
  4. @file-success="onFileSuccess"
  5. @file-added="onFileAdded"
  6. class="uploader-example"
  7. :auto-start="auto"
  8. :fileStatusText="status"
  9. ref="uploadList"
  10. >
  11. <uploader-unsupport></uploader-unsupport>
  12. <uploader-drop>
  13. <uploader-btn :attrs="{ accept: Object.type }">
  14. 上传视频
  15. </uploader-btn>
  16. </uploader-drop>
  17. <uploader-list
  18. id="shi"
  19. class="uploaderList"
  20. style="width: 240px">
  21. </uploader-list>
  22. </uploader>

二、script标签里 :options="optionsV.optionsV"

  1. const optionsV = reactive({
  2. optionsV: {
  3. target: "/api/mobilehome/up/upload",
  4. testChunks: false,
  5. chunkSize: 1024 * 1024 * 2, //1MB
  6. simultaneousUploads: 3, //并发上传数
  7. checkChunkUploadedByResponse: (chunk, message) => {
  8. let obj = JSON.parse(message);
  9. if (obj.isExist) {
  10. //它为true的时候标明文件已经存在了
  11. statusTextMap.success = "秒传文件";
  12. return true;
  13. }
  14. return (obj.uploaded || []).indexOf(chunk.offset + 1) >= 0;
  15. },
  16. },
  17. });

三、script标签里  @file-success="onFileSuccess"

  1. const onFileSuccess = async (rootFile, file, response, chunk) => {
  2. //
  3. let reader = new FileReader();
  4. reader.readAsDataURL(file.file);
  5. reader.onload = (e) => {
  6. // cVideoPreview.value = reader.result; //应该是上传的地址
  7. };
  8. // 视频唯一标识
  9. const video_identifier = reactive({
  10. value: "",
  11. });
  12. const fileSize = reactive({
  13. value: "",
  14. });
  15. // 获取视频时长
  16. let url = URL.createObjectURL(file.file);
  17. // console.log(url)
  18. let audioEle = new Audio(url);
  19. // console.log(audioEle)
  20. audioEle.addEventListener("loadedmetadata", () => {
  21. // 视频时长值的获取要等到这个匿名函数执行完毕才产生
  22. var result = audioEle.duration; //得到时长为秒,小数,182.36
  23. result = parseInt(audioEle.duration);
  24. // console.log(result)
  25. cVideoTime.value = result; //存放视频时长
  26. // console.log(this.video_time)
  27. });
  28. let resp = JSON.parse(response);
  29. if (resp.code === 0 && resp.merge === false) {
  30. // console.log('上传成功,不需要合并')
  31. console.log(resp);
  32. cVideoPreview.value = resp.filePath;
  33. message.success("视频上传成功");
  34. checkShow.value = true;
  35. //uploaderSuccessShow.value = false;
  36. console.log(status);
  37. } else {
  38. let { data } = await axios.post("/mobilehome/up/upload?action=merge", {
  39. filename: file.name, //文件名称
  40. identifier: file.uniqueIdentifier, //文件唯一标识
  41. totalSize: file.size, //文件文大小
  42. totalChunks: chunk.offset + 1, //文件总分片数
  43. });
  44. console.log(data);
  45. if (data.code === 0) {
  46. console.log(data);
  47. cVideoPreview.value = data.filePath;
  48. video_identifier.value = file.uniqueIdentifier;
  49. fileSize.value = file.size;
  50. // console.log('上传成功')
  51. message.success("视频上传成功");
  52. checkShow.value = true;
  53. // uploaderSuccessShow.value = false;
  54. } else {
  55. message.success("视频上传失败");
  56. // uploaderSuccessShow.value = false;
  57. CloseShow.value = true;
  58. }
  59. // catch((error) => {
  60. // console.log(error);
  61. // alert(error);
  62. // });
  63. }
  64. };

 四、script标签里 @file-added="onFileAdded"

  1. const onFileAdded = (file) => {
  2. computeMD5(file);
  3. };

五、上边函数中的computeMD5(file)

  1. const computeMD5 = (file) => {
  2. let blobSlice =
  3. File.prototype.slice ||
  4. File.prototype.mozSlice ||
  5. File.prototype.webkitSlice,
  6. chunkSize = 2097152, //2MB
  7. chunks = Math.ceil(file.size / chunkSize),
  8. currentChunk = 0,
  9. spark = new SparkMD5.ArrayBuffer(),
  10. fileReader = new FileReader();
  11. let time = new Date().getTime();
  12. file.cmd5 = true; //文件状态为“计算md5...”
  13. fileReader.onload = (e) => {
  14. spark.append(e.target.result); // Append array buffer
  15. currentChunk++;
  16. if (currentChunk < chunks) {
  17. console.log(
  18. `第${currentChunk}分片解析完成, 开始第${
  19. currentChunk + 1
  20. } / ${chunks}分片解析`
  21. );
  22. loadNext();
  23. } else {
  24. console.log("finished loading");
  25. let md5 = spark.end(); //得到md5
  26. console.log(
  27. `MD5计算完成:${file.name} \nMD5:${md5} \n分片:${chunks} 大小:${
  28. file.size
  29. } 用时:${new Date().getTime() - time} ms`
  30. );
  31. spark.destroy(); //释放缓存
  32. file.uniqueIdentifier = md5; //将文件md5赋值给文件唯一标识
  33. console.log(md5);
  34. console.log(file.uniqueIdentifier);
  35. file.cmd5 = false; //取消计算md5状态
  36. file.resume(); //开始上传
  37. auto.value = true; //文件计算完成开始上传
  38. }
  39. };
  40. fileReader.onerror = () => {
  41. console.warn("oops, something went wrong.");
  42. file.cancel();
  43. };
  44. let loadNext = () => {
  45. let start = currentChunk * chunkSize,
  46. end = start + chunkSize >= file.size ? file.size : start + chunkSize;
  47. fileReader.readAsArrayBuffer(blobSlice.call(file.file, start, end));
  48. };
  49. loadNext();
  50. };

六、script标签里 :auto-start="auto"

  1. // 文件添加后是否立即上传,默认不,分片完成之后变为true
  2. const auto = ref(false);

六、script标签里 :fileStatusText="status"

  1. const status = reactive({
  2. success: "已成功",
  3. error: "已失败",
  4. uploading: "加载中",
  5. paused: "暂停",
  6. waiting: "请稍等",
  7. });

七、script中的 ref="uploadList"

  1. // 清空上传列表
  2. const uploadList = ref("uploadList");

八、点击上传视频

  1. // 点击上传视频
  2. const addModalVidel = () => {
  3. addVisibleVideo.value = true;
  4. formState.username = ""; //输入的名字清空
  5. uploadVideoShow.value = false; //展示视频的地方清空
  6. auto.value = false;
  7. //利用原生js获取要清空的列表
  8. var ul = document.getElementById("shi");
  9. if (uploadList.value.$el.firstElementChild.firstElementChild) {
  10. //uploadList.value.$el.firstElementChild.firstElementChild.remove()
  11. // ul.firstElementChild.firstElementChild.style.width = "0";
  12. // ul.firstElementChild.firstElementChild.style.height = "0";
  13. ul.firstElementChild.firstElementChild.remove();//移除之前上传的视频列表
  14. }
  15. };

九、点击提交按钮

  1. const onFinish = async (values) => {
  2. let fd = new FormData();
  3. fd.append("name", formState.username);//视频名字
  4. fd.append("file_url", cVideoPreview.value);//视频文件
  5. fd.append("video_time", cVideoTime.value); //视频时长
  6. // fd.append('md5',video_identifier.value)
  7. // fd.append('filesize',fileSize.value)
  8. let { data } = await axios.post(`/mobilehome/materialcenter/center_add`, fd);
  9. console.log(data);
  10. if (data.code == 0) {
  11. message.success(data.msg);
  12. getTableList();//更新列表
  13. addVisibleVideo.value = false;//上传视频弹框消失
  14. } else {
  15. message.error(data.msg);
  16. }
  17. };

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/267824
推荐阅读
相关标签
  

闽ICP备14008679号