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

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));

后记

相关推荐
Lovereo4 小时前
我的目标检测性能优化之路:预算不够、GPU 没有、但性能我得要
前端
蒙娜丽宁4 小时前
Rust 与 WebAssembly:构建高效前端应用的全流程复盘
前端·rust·wasm
这儿有一堆花4 小时前
使用 Actix-web 开发高性能 Web 服务
前端·数据库
豆苗学前端4 小时前
10分钟带你入门websocket,并实现一个在线多人聊天室
前端·javascript·后端
白水清风4 小时前
Vue3之渲染器
前端·vue.js·面试
刘永胜是我4 小时前
解决Volta环境下npm全局包卸载失败:一次深入排查之旅
前端·node.js
白水清风4 小时前
Vue3之组件化
前端·vue.js·面试
边洛洛4 小时前
解决[PM2][ERROR] Script not found: D:\projects\xxx\start
前端·javascript
农夫山泉的小黑5 小时前
【DeepSeek帮我准备前端面试100问】(十八)Reflect在vue3的使用
前端·面试
Achieve前端实验室5 小时前
【每日一面】手写防抖函数
前端·面试·node.js