当前位置:网站首页>Javscript gets the real suffix of the file
Javscript gets the real suffix of the file
2022-04-23 07:35:00 【XIX (procrastination)】
List of articles
javscript Get the real suffix of the file
Preface : Wrong acquisition method
What were the steps to get files in the past ?
The first thought is the file obtained according to the input of the file , Then get the attributes of the file , Then get the type according to the attributes of the file , Then get the format by intercepting .
Such as :
<input name="file" type="file" name="" id="fileid">
let file = document.getElementById("fileid");
file.addEventListener("change",function(){
console.log(file.files[0])
})
By intercepting type Get the property value of png.
But the problem is , If I replace the file extension , The format is no longer correct .
When I change the file extension from png Change to jpg when , Output the following format
This format is wrong , Just change the suffix , But the original file type is actually png, But then we get the wrong output jpg
1: adopt javascript Get the correct suffix
To get the correct suffix , Here you need to get the decimal format of the file
Then convert the decimal data to hexadecimal , Determine the file format
As always, , First get type by file Of input Elements
let file = document.getElementById("fileid");
When selecting a file , Will trigger change event
file.addEventListener("change",function(){
})
And then get the file
let file = file.files[0];
Next you need to use a new object FileReader
let read = new FileReader();
Then use the... Of the object readAsArrayBuffer Method
file.addEventListener("change",function(){
let read = new FileReader();
read.readAsArrayBuffer(file.files[0])
read.onload = function(){
console.log(read.result)
}
})
readAsArrayBuffer The read file will be stored in result Properties of the , But you need onload After loading successfully, get .
Now , You will get the following data :
Expand to view :
Get the first four data of the array :
let arr = (new Uint8Array(read.result)).subarray(0, 4);
Combine these data into a string and turn it into 16 Hexadecimal data for comparison :
for(let i = 0;i<arr.length;i++){
msg+=arr[i].toString(16)
}
obtain :89504e47(msg Value )
among :
- png Hexadecimal :89 50 4E 47
- jpg Hexadecimal :FF D8 FF E0
- gif Hexadecimal :47 49 46 38
Next , You can check the form to determine the file type :
let file = document.getElementById("fileid");
file.addEventListener("change",function(){
let read = new FileReader();
read.readAsArrayBuffer(file.files[0])
read.onload = function(){
let arr = (new Uint8Array(read.result)).subarray(0, 4);
let msg = "";
for(let i = 0;i<arr.length;i++){
msg+=arr[i].toString(16)
}
switch(msg){
case '89504e47':
console.log("png");
break;
case 'ffd8ffe0':
console.log("jpg");
break;
case '47494638':
console.log("gif");
break;
}
}
})
Need to get All file data types , Please check the leftmost side 《 The column 》 Method to get , All types have been sorted out , And packed it up json file .
版权声明
本文为[XIX (procrastination)]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230618084799.html
边栏推荐
猜你喜欢
Tensorflow安装后ImportError: DLL load failed: 找不到指定的模块,且国内安装缓慢
H5案例开发
菜菜的并发编程笔记 |(九)异步IO实现并发爬虫加速
Meishe technology launches professional video editing solution for desktop -- Meiying PC version
海南凤凰机场智能通信解决方案
可视化之路(十一)matplotlib颜色详解
自定义classloader并实现热部署-使用loadClass
北峰油气田自组网无线通信对讲系统解决方案
# 可视化常见绘图(二)折线图
江宁医院DMR系统解决方案
随机推荐
学习笔记7-深度神经网络优化
go语言:在函数间传递切片
学习资料
如何将进程绑定到指定的CPU上
按需引入vant组件
华为云MVP邮件
字节数仓实习生面试sql题
jvm知识点汇总-持续更新
pytorch:关于GradReverseLayer实现的一个坑
The difference between null and undefined
Statement of American photography technology suing Tianmu media for using volcanic engine infringement code
HuggingFace
小程序换行符\n失效问题解决-日常踩坑
字节跳动2020秋招编程题:根据工号快速找到自己的排名
P1390 公约数的和(莫比乌斯反演)
presto日期函数的使用
北峰油气田自组网无线通信对讲系统解决方案
启动mqbroker.cmd失败解决方法
记录阿里云服务器挖矿程序处理
Lead the industry trend with intelligent production! American camera intelligent video production platform unveiled at 2021 world Ultra HD Video Industry Development Conference