file、base64 相互转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
function fileToBase64(file) { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = () => resolve(reader.result); reader.onerror = (error) => reject(error); }); }
function base64toFile(base64Data, fileName) { let data = base64Data.split(","), mime = data[0].match(/:(.*?);/)[1], byteString = atob(data[1]), byteLength = byteString.length, uInt8Array = new Uint8Array(byteLength); while (n--) { uInt8Array[n] = byteString.charCodeAt(n); } return new File([uInt8Array], fileName, { type: mime }); }
|
File、Blob 相互转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
function blobToFile(blob, fileName) { blob.lastModifiedDate = new Date(); blob.name = fileName; return blob; }
function fileToBlob(file) { let reader = new FileReader();
return new Promise((resolve) => { reader.addEventListener("load", (e) => { let blob; if (typeof e.target.result === "object") { blob = new Blob([e.target.result]); } else { blob = e.target.result; } console.log(Object.prototype.toString.call(blob)); resolve(blob); });
reader.readAsArrayBuffer(file); }); }
|
base64、Blob 相互转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
function baseURLtoBlob(base64) { let arr = base64.split(","), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); while (n--) { u8arr[n] = bstr.charCodeAt(n); } return new Blob([u8arr], { type: mime }); }
function blobToBase64(blob, callback) { let reader = new FileReader(); reader.readAsDataURL(blob); reader.onload = function (e) { callback(e.target.result); }; }
|
ArrayBuffer、File 相互转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
function bufferToFile(buffer, filename) { return new File([buffer], filename); }
function fileToBuffer(file, cb) { let fr = new FileReader(); let filename = file.name; fr.readAsArrayBuffer(file); fr.addEventListener( "loadend", (e) => { let buffer = e.target.result; cb(buffer, filename); }, false ); }
|
ArrayBuffer、base64 相互转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
function bufferToBase64(buffer) { return "data:image/jpeg;base64," + window.btoa(String.fromCharCode(...new Uint8Array(buffer))); }
function base64ToBuffer(base64) { let binary_string = window.atob(base64); let len = binary_string.length; let bytes = new Uint8Array(len); for (let i = 0; i < len; i++) { bytes[i] = binary_string.charCodeAt(i); } return bytes.buffer; }
|
ArrayBuffer、Blob 相互转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
function bufferToBlob(buffer, mimeType = "") { return new Blob([buffer], { type: mimeType }); }
function blobToBuffer(blob, cb) { let fr = new FileReader(); let type = blob.type; fr.readAsArrayBuffer(blob); fr.addEventListener( "loadend", (e) => { let buffer = e.target.result; cb(buffer, type); }, false ); }
|
Blob 的应用
- 大文件分割
slice()
方法接受三个参数,起始偏移量,结束偏移量,还有可选的 mime
类型。如果 mime
类型,没有设置,那么新的 Blob
对象的 mime
类型和父级一样。 - 当要上传大文件的时候,此方法非常有用,可以将大文件分割分段,然后各自上传,因为分割之后的
Blob
对象和原始的是独立存在的。
不过目前浏览器实现此方法还没有统一,火狐使用的是 mozSlice()
,Chrome 使用的是 webkitSlice()
,其他浏览器则正常的方式 slice()
。
1 2 3 4 5 6 7 8 9 10 11
| function sliceBlob(blob, start, end, type) { type = type || blob.type; if (blob.mozSlice) { return blob.mozSlice(start, end, type); } else if (blob.webkitSlice) { return blob.webkitSlice(start, end, type); } else { throw new Error("This doesn't work!"); } }
|