arrayBuffer讀取本地文件


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <input type="file" name="" id="fileInput">
    <script>
        //獲取元素
        var fileInput = document.querySelector("#fileInput");
        //監聽事件
        fileInput.onchange = function() {
            //獲取文件
            var file = this.files[0];
            //讀取文件
            var fileReader = new FileReader();
            //轉換文件為ArrayBuffer
            fileReader.readAsArrayBuffer(file);
            //監聽完成事件
            fileReader.onload = function() {
                //打印arraybuffer的字節長度 也是文件的大小 到了這一步就可以使用arraybuffer進行
                //文件的修改之類的操作了
                console.log(fileReader.result.byteLength);
            }
        }
    </script>
</body>

</html>

具體操作按照需要使用的情況而定

<! DOCTYPE  html>
< html  lang= "en">

< head>
    < meta  charset= "UTF-8">
    < meta  name= "viewport"  content= "width=device-width, initial-scale=1.0">
    < meta  http-equiv= "X-UA-Compatible"  content= "ie=edge">
    < title>Document</ title>
</ head>

< body>
    < input  type= "file"  name= ""  id= "fileInput">
    < script>
         //獲取元素
         var  fileInput  =  document. querySelector( "#fileInput");
         //監聽事件
         fileInput. onchange  =  function() {
             //獲取文件
             var  file  =  this. files[ 0];
             //讀取文件
             var  fileReader  =  new  FileReader();
             //轉換文件為ArrayBuffer
             fileReader. readAsArrayBuffer( file);
             //監聽完成事件
             fileReader. onload  =  function() {
                 //打印arraybuffer的字節長度 也是文件的大小 到了這一步就可以使用arraybuffer進行
                 //文件的修改之類的操作了
                 console. log( fileReader. result. byteLength);
            }
        }
    </ script>
</ body>

</ html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM