上传插件,不包含上传的前端实现,只提供后端接口等,其他地方接入插件上传。包括上传进度、断点续传等

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import SparkMD5 from 'spark-md5'
  2. export default function getFilemd5sum(ofile) {
  3. return new Promise((resolve, reject) => {
  4. let file = ofile;
  5. let tmp_md5;
  6. let blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
  7. // file = this.files[0],
  8. chunkSize = 8097152, // Read in chunks of 2MB
  9. chunks = Math.ceil(file.size / chunkSize),
  10. currentChunk = 0,
  11. spark = new SparkMD5.ArrayBuffer(),
  12. fileReader = new FileReader();
  13. function loadNext() {
  14. var start = currentChunk * chunkSize,
  15. end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;
  16. fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
  17. }
  18. fileReader.onload = function (e) {
  19. spark.append(e.target.result); // Append array buffer
  20. currentChunk++;
  21. if (currentChunk < chunks) {
  22. loadNext();
  23. } else {
  24. tmp_md5 = spark.end();
  25. resolve(tmp_md5)
  26. }
  27. };
  28. fileReader.onerror = function () {
  29. reject('error');
  30. };
  31. loadNext();
  32. })
  33. }