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

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