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

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

后记

相关推荐
灵感__idea6 分钟前
JavaScript高级程序设计(第5版):好的编程就是掌控感
前端·javascript·程序员
烛阴1 小时前
Mix
前端·webgl
代码续发1 小时前
前端组件梳理
前端
试图让你心动2 小时前
原生input添加删除图标类似vue里面移入显示删除[jquery]
前端·vue.js·jquery
陈不知代码2 小时前
uniapp创建vue3+ts+pinia+sass项目
前端·uni-app·sass
小王码农记3 小时前
sass中@mixin与 @include
前端·sass
陈琦鹏3 小时前
轻松管理 WebSocket 连接!easy-websocket-client
前端·vue.js·websocket
hui函数3 小时前
掌握JavaScript函数封装与作用域
前端·javascript
行板Andante3 小时前
前端设计中如何在鼠标悬浮时同步修改块内样式
前端
Carlos_sam4 小时前
Opnelayers:ol-wind之Field 类属性和方法详解
前端·javascript