FileReader
: 读取文件内容的api,,,在前端处理上传的文件,,比如预览图片
- readAsDataURL(file) : 读取为base64编码的 data url
- readAsText() : 读取为文本
- readAsArrayBuffer() : 读取为二进制
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><input type="file" name="file" onchange="previewImage(event)"><script>function previewImage(e){console.log(e.target.files[0],e,"e")const file = e.target.files[0]var fileReader = new FileReader();// 读完了,会执行fileReader.onload = function (e){var image = new Image();image.src = e.target.resultimage.onload = function (){document.documentElement.appendChild(image)}}fileReader.readAsDataURL(file)}</script></body>
</html>