当前位置:网站首页>JS file block to Base64 text
JS file block to Base64 text
2022-06-30 04:10:00 【Runqing】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<!-- FileList Object each selected file . If multiple Property is not specified , Then the list has only one member . -->
<input type="file" multiple="multiple" id="file" />
<script>
window.onload = function () {
handleSelectFile()
}
function handleSelectFile () {
var fileEle = document.getElementById('file')
fileEle.onchange = async function (e) {
var files = e.target.files
let name, size, type
if (files && files.length > 0) {
filesTask(files).then(res => {
console.log('res', res)
})
}
}
}
async function filesTask (files, path) {
const filesResult = []
var file;
// Traverse all files
for (var i = 0; i < files.length; i++) {
file = files[i];
console.log(`file${
i}:`, file)
const base64FileArr = await fileToBase64Arr(file)
filesResult.push(base64FileArr)
}
return filesResult
}
function fileToBase64Arr (file) {
const fileSlice = sliceFile(file)
// console.log('fileSlice', fileSlice)
return readFileSlice(fileSlice)
}
// size Set slice size , Current unit is 1kb, It can be done by unit and size adjustment
function sliceFile(file, unit = 100) {
const size = unit * 1024
const result = []
let start = 0, current = 0, end = file.size
while(start < end) {
if((end-start) > size) {
current = start + size
} else {
current = end
}
result.push(file.slice(start, current))
start = current
}
return result
}
async function readFileSlice(fileSlice) {
const result = []
for await (let fileText of fileSlice.map(file => readFile(file))){
// console.log('fileText', fileText)
result.push(fileText)
}
return result
}
function readFile (file) {
return new Promise(resolve => {
var reader = new FileReader();
// When FileReader The way to read the file is readAsArrayBuffer, readAsBinaryString, readAsDataURL perhaps readAsText When , It will trigger a load (en-US) event .
reader.onload = function (res) {
// console.log('res', res)
// ①. hold 3 Bytes become 4 Bytes .② Every time 76 Characters plus a newline .③. The last terminator also deals with . therefore base64 After the coding , The byte length will be larger than the original 1/3(1/76 Just ignore )
const result = event.target.result
// console.log('result', result)
// console.log(' length ',result.length)
resolve(result)
}
// readAsDataURL Method will read the specified Blob or File object . When the read operation is complete ,readyState It will become finished DONE, And trigger loadend (en-US) event , meanwhile result Property will contain a data:URL Format string (base64 code ) To represent the contents of the read file .
reader.readAsDataURL(file)
})
}
</script>
</body>
</html>
边栏推荐
- Unity échappe à l'entrée de caractères lors de l'entrée de chaînes dans l'éditeur
- [operation] getting started with MySQL on May 23, 2022
- .NET 7 的 JWT 配置太方便了!
- DO280私有仓库持久存储与章节实验
- Idea grey screen problem
- MySQL updates JSON string in array form
- base64.c
- Day 9 script and resource management
- (03).NET MAUI实战 基础控件
- Solve the problem of Navicat connecting to the database
猜你喜欢

第十二天 进阶编程技术

Day 9 script and resource management

Interface test tool postman

dotnet-exec 0.5.0 released

Machine learning notes

I get n offers in two months. I don't have any difficult interviewers here

Day 10 data saving and loading

技术分享| 融合调度中的广播功能设计

I spent three years in a big factory outsourcing, which subverted my understanding!

The school training needs to make a registration page. It needs to open the database and save the contents entered on the registration page into the database
随机推荐
How to analyze and solve the problem of easycvr kernel port error through process startup?
thinkphp5实现导入功能
毕业设计EMS办公管理系统(B/S结构)+J2EE+SQLserver8.0
Geometric objects in shapely
[Thesis reading | deep reading] role2vec:role based graph embeddings
学校实训要做一个注册页面,要打开数据库把注册页面输入的内容存进数据库但是
Use ideal to connect to the database. The results show some warnings. How to deal with this part
(03). Net Maui actual combat basic control
. Net 7 JWT configuration is too convenient!
errno和perror
JS proxy
NER中BiLSTM-CRF解读score_sentence
Pig-Latin (UVA492)
Slam mapping, automatic navigation and obstacle avoidance based on ROS (bingda robot)
Knowledge - how to build rapport in sales with 3 simple skills
在大厂外包呆了三年,颠覆了我的认知!
《机器人SLAM导航核心技术与实战》第1季:第0章_SLAM发展综述
管道实现进程间通信之命名管道
Project safety and quality
(04). Net Maui actual MVVM