前端也要学的“文件转换规则”知识

Text 作为起点

Text 至 Blob

javascript 复制代码
const blob = new Blob(["Hello, world!"], {
    type: "text/plain"
});

console.log(blob)

Text 至 File

javascript 复制代码
const file = new File(["Hello, world!"], "hello.txt", {
    type: "text/plain"
});

console.log(file)

Text 至 Base64

0-255以内的字符,属于 Latin1 字符集

javascript 复制代码
const text = "Hello World";

console.log(window.btoa(text));

包含中文等...,属于 Unicode 字符集

encodeURIComponent
javascript 复制代码
const text = "w爱n";
let encodedString  = encodeURIComponent(text);

console.log(window.btoa(encodedString));

encodeURIComponent 要配合 encodeURIComponent 一同使用哦!!!

TextEncoder 配合 fromCharCode
javascript 复制代码
const text = "w爱n";
const buffer = new TextEncoder().encode(text)
let chats = [];
for (let i = 0; i < buffer.length; i++) {
chats.push(String.fromCharCode(buffer[i]));
}
chats = chats.join("")

console.log(window.btoa(chats));

FileReader

javascript 复制代码
const text = "w爱n";
const file = new File([text], "text.txt", { type: "text/plain" });
const render = new FileReader();
render.readAsDataURL(file);
render.onload = function () {
    console.log(render.result);
}

Blob作为起点

Blob 至 File

javascript 复制代码
const blob = new Blob(["Hello, world!"], {
    type: "text/plain"
});
const file = new File([blob], "hello.txt", {
    type: "text/plain"
});

console.log(file)

Blob 至 Text

javascript 复制代码
const blob = new Blob(["Hello, world!"], {
    type: "text/plain"
});
const reader = new FileReader();
reader.onload = function () {
    console.log(reader.result);
};
reader.readAsText(blob);

Blob 至 Base64

javascript 复制代码
const blob = new Blob(["Hello, world!"], {
    type: "text/plain"
});
const reader = new FileReader();
reader.onload = function () {
    console.log(reader.result);
};
reader.readAsDataURL(blob);

Blob 至 ArrayBuffer

javascript 复制代码
const blob = new Blob(["Hello, world!"], {
    type: "text/plain"
});
const reader = new FileReader();
reader.onload = function () {
    console.log(reader.result);
};
reader.readAsArrayBuffer(blob);

Blob 至 ObjectURL

javascript 复制代码
const blob = new Blob(["Hello, world!"], {
    type: "text/plain"
});
const objectUrl = URL.createObjectURL(blob);

console.log(objectUrl);

File作为起点

File 至 Text

javascript 复制代码
const file = new File(["Hello, world!"], "hello.txt", {
    type: "text/plain"
});
const reader = new FileReader();
reader.onload = function () {
    console.log(reader.result);
};
reader.readAsText(file);

File 至 Base64

javascript 复制代码
const file = new File(["Hello, world!"], "hello.txt", {
    type: "text/plain"
});
const reader = new FileReader();
reader.onload = function () {
    console.log(reader.result);
};
reader.readAsDataURL(file);

File 至 ArrayBuffer

javascript 复制代码
 const file = new File(["Hello, world!"], "hello.txt", {
     type: "text/plain"
 });
const reader = new FileReader();
reader.onload = function () {
    console.log(reader.result);
};
reader.readAsArrayBuffer(file);

File 至 ObjectURL

javascript 复制代码
const file = new File(["Hello, world!"], "hello.txt", {
    type: "text/plain"
});
const objectUrl = URL.createObjectURL(file);

console.log(objectUrl);

ArrayBuffer 作为起点

ArrayBuffer 至 Blob

javascript 复制代码
const buffer = new ArrayBuffer(8);
const blob = new Blob([buffer], {
    type: "text/plain"
});

console.log(blob)

ArrayBuffer 至 File

javascript 复制代码
const buffer = new ArrayBuffer(8);
const file = new File([buffer], "file.txt", {
    type: "text/plain",
});

console.log(file)

Base64作为起点

Base64 至 ArrayBuffer

javascript 复制代码
const base64String = "d+eIsW4=";
const binaryString = window.atob(base64String);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
    bytes[i] = binaryString.charCodeAt(i);
}
console.log(bytes.buffer);

Base64 至 Text

0-255以内的字符,属于 Latin1 字符集

javascript 复制代码
const base64String = "SGVsbG8gV29ybGQ=";

console.log(window.atob(base64String));

包含中文等...,属于 Unicode 字符集

javascript 复制代码
const text = "dyVFNyU4OCVCMW4=";

let encodedString = decodeURIComponent(window.atob(text));

console.log(encodedString);

encodeURIComponent 要配合 encodeURIComponent 一同使用哦!!!

TextDecoder 配合 charCodeAt
javascript 复制代码
const base64String = "d+eIsW4="; 
const binaryString = window.atob(base64String);
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
    bytes[i] = binaryString.charCodeAt(i);
}
console.log(new TextDecoder().decode(bytes));

后记

相关推荐
ZXT3 分钟前
promise & async await总结
前端
Jerry说前后端3 分钟前
RecyclerView 性能优化:从原理到实践的深度优化方案
android·前端·性能优化
画个太阳作晴天11 分钟前
A12预装app
linux·服务器·前端
77238938 分钟前
解决 Microsoft Edge 显示“由你的组织管理”问题
前端·microsoft·edge
烛阴1 小时前
前端必会:如何创建一个可随时取消的定时器
前端·javascript·typescript
JarvanMo1 小时前
Swift 应用在安卓系统上会怎么样?
前端
LinXunFeng2 小时前
Flutter - 详情页 TabBar 与模块联动?秒了!
前端·flutter·开源
萌萌哒草头将军2 小时前
Oxc 最新 Transformer Alpha 功能速览! 🚀🚀🚀
前端·javascript·vue.js
Justinc.2 小时前
HTML5新增属性
前端·html·html5
1024小神3 小时前
nextjs项目build导出静态文件
前端·javascript