当前位置:网站首页>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
边栏推荐
- How does the public and Private Integrated walkie talkie realize cooperative work under multi-mode communication?
- 可视化常见绘图(五)散点图
- Metro wireless intercom system
- Unable to determine the device handle for GPU 0000:02:00.0: GPU is lost.
- Wireless communication system for large-scale sports events
- 免费开源智能充电桩物联网SAAS云平台
- 记录一个查询兼容性的网站,String.replaceAll()兼容性报错
- 北峰油气田自组网无线通信对讲系统解决方案
- 字节跳动2020秋招编程题:根据工号快速找到自己的排名
- Machine vision series (01) -- Overview
猜你喜欢
Lead the industry trend with intelligent production! American camera intelligent video production platform unveiled at 2021 world Ultra HD Video Industry Development Conference
el-date-picker中自定义快捷选项picker-options,动态设置禁用日期
USO technology was invited to share the technical framework and challenges of AI synthetic virtual characters at lvson2020 conference
菜菜的刷题日记 | 蓝桥杯 — 十六进制转八进制(纯手撕版)附进制转换笔记
The people of Beifeng have been taking action
关于'enum'枚举类型以及结构体的问题。
ES6之箭头函数细谈
学习笔记5-梯度爆炸和梯度消失(K折交叉验证)
quill-editor图片缩放、在一个页面使用多个富文本框、quill-editor上传图片地址为服务器地址
海南凤凰机场智能通信解决方案
随机推荐
DMR system solution of Kaiyuan MINGTING hotel of Fengqiao University
go语言数组操作
可视化常见问题解决方案(七)画图刻度设置解决方案
菜菜的并发编程笔记 |(五)线程安全问题以及Lock解决方案
使用compressorjs压缩图片,优化功能,压缩所有格式的图片
学习笔记7-深度神经网络优化
Int8 quantification and inference of onnx model using TRT
How does the public and Private Integrated walkie talkie realize cooperative work under multi-mode communication?
PyTorch 22. Pytorch common code snippet collection
Draw margin curve in arcface
PyTorch 14. Module class
记录一下使用v-print中遇到的问题
Discussion on the outline of short video technology
应急医疗通讯解决方案|MESH无线自组网系统
可视化常见绘图(四)柱状图
按需引入vant组件
Machine vision series (02) -- tensorflow2 3 + win10 + GPU installation
Machine vision series (01) -- Overview
启动mqbroker.cmd失败解决方法
特殊成员与魔法方法