1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- import { SparkMD5 } from 'spark-md5'
-
- export function get_filemd5sum(ofile) {
- var file = ofile;
- var tmp_md5;
- var blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice,
- // file = this.files[0],
- chunkSize = 8097152, // Read in chunks of 2MB
- chunks = Math.ceil(file.size / chunkSize),
- currentChunk = 0,
- spark = new SparkMD5.ArrayBuffer(),
- fileReader = new FileReader();
-
- fileReader.onload = function(e) {
- spark.append(e.target.result); // Append array buffer
- currentChunk++;
-
- if (currentChunk < chunks) {
- loadNext();
- } else {
- tmp_md5 = spark.end();
- return tmp_md5
- }
- };
-
- fileReader.onerror = function() {
- console.warn('oops, something went wrong.');
- };
-
- function loadNext() {
- var start = currentChunk * chunkSize,
- end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize;
- fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
- }
- loadNext();
- }
-
- export function getBase64(file) {
- var reader = new FileReader();
- reader.readAsDataURL(file);
- reader.onload = function (e) {
- return this.result.substring(this.result.indexOf(',')+1);
- }
- }
|