当前位置:网站首页>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>
边栏推荐
- Default value of JS parameter
- Node red series (28): communication with Siemens PLC based on OPC UA node
- 管道实现进程间通信之命名管道
- Analysis of similarities and differences of various merged features (Union, merge, append, resolve) in ArcGIS
- Graduation project EMS office management system (b/s structure) +j2ee+sqlserver8.0
- Pytorch Profiler+ Tensorboard + VS Code
- 两个月拿到N个offer,什么难搞的面试官在我这里都不算事
- How to analyze and solve the problem of easycvr kernel port error through process startup?
- Find the interface and add parameters to the form
- Unity 在編輯器中輸入字符串時,轉義字符的輸入
猜你喜欢

You know AI, database and computer system

(04).NET MAUI实战 MVVM

(Reprinted) an article will take you to understand the reproducing kernel Hilbert space (RKHS) and various spaces
![[punch in - Blue Bridge Cup] day 4--------- split ('') cannot be used. There is a space after the last number of test cases. Split ()](/img/00/3793a236ee37085cb47dbfa1f0dbff.jpg)
[punch in - Blue Bridge Cup] day 4--------- split ('') cannot be used. There is a space after the last number of test cases. Split ()

巧用 Bitmap 实现亿级海量数据统计
![[fuzzy neural network prediction] water quality prediction based on fuzzy neural network, including Matlab source code](/img/88/038826ec6d16c8eb04d9ef2e01d47a.png)
[fuzzy neural network prediction] water quality prediction based on fuzzy neural network, including Matlab source code

Share an example of a simple MapReduce method using a virtual machine

毕业设计EMS办公管理系统(B/S结构)+J2EE+SQLserver8.0
![[Thesis reading | deep reading] role2vec:role based graph embeddings](/img/69/c94700fbbbda20df4e54803c703b48.png)
[Thesis reading | deep reading] role2vec:role based graph embeddings

技术分享| 融合调度中的广播功能设计
随机推荐
UML diagrams and list collections
Green new power and "zero" burden of computing power -- JASMINER X4 series is popular
[note] May 23, 2022 MySQL
Technology sharing | broadcast function design in integrated dispatching
2021-07-14
DRF -- nested serializer (multi table joint query)
第十一天 脚本与游戏AI
AI落地的新范式,就“藏”在下一场软件基础设施的重大升级里
Interface test tool postman
[fuzzy neural network prediction] water quality prediction based on fuzzy neural network, including Matlab source code
如何通过进程启动来分析和解决EasyCVR内核端口报错问题?
关于智能视觉组上的机械臂
If you encounter problems when using spark for the first time, please ask for help
el-upload上傳文件(手動上傳,自動上傳,上傳進度)
I get n offers in two months. I don't have any difficult interviewers here
【模糊神经网络预测】基于模糊神经网络实现水质预测含Matlab源码
NER中BiLSTM-CRF解读score_sentence
(04).NET MAUI实战 MVVM
[operation] MySQL query on May 24, 2022
[image fusion] multi focus and multi spectral image fusion based on cross bilateral filter and weighted average with matlab code