fileReader對象讀取txt文件亂碼問題 以及如何獲取文件的url路徑(絕對路徑)


        <input type="file" @change="aaa($event)">
        <div id="hi"></div>

以上是html(用的是vue,所以用@綁定的)

今天想用js讀取txt文件,但是一直亂碼,后來查到,把reader.readAsText(file,'gb2312')中的編碼格式設置成gb2312就不亂碼了

js:
       aaa(event){
            var self=this,
            files=event.target.files,
            file,
            htmlBox=document.getElementById("hi");
            if(files&&files.length>0){
                file=files[0];
                console.log(file)
            }
            let reader=new FileReader();
            
            reader.onload = function (e) {
           console.log(e.target.result);
                htmlBox.innerHTML=e.target.result;
        }
            // reader.readAsDataURL(file);
            reader.readAsText(file,'gb2312')
        }

 如何獲取文件的url路徑

js

       getObjectURL(file) {//創建url路徑
            let url = null;
            if (window.createObjectURL != undefined) {
                // basic
                url = window.createObjectURL(file);
            } else if (window.URL != undefined) {
                // mozilla(firefox)
                url = window.URL.createObjectURL(file);
            } else if (window.webkitURL != undefined) {
                // webkit or chrome
                url = window.webkitURL.createObjectURL(file);
            }
            return url;
        },       

 

 


免責聲明!

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



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