首页 > 其他 > 详细

读存文件

时间:2020-04-17 12:11:23      阅读:52      评论:0      收藏:0      [点我收藏+]

《读取》

<input type="file" onchange="upload(this)" />

     function upload(input) {  
            //支持chrome IE10  
            if (window.FileReader) {  
                var file = input.files[0];  
                filename = file.name.split(".")[0];  
                var reader = new FileReader();  
                reader.onload = function() {  
                    console.log(this.result);  
                }  
                reader.readAsText(file);  
            }   
            //支持IE 7 8 9 10  
            else if (typeof window.ActiveXObject != ‘undefined‘){  
                var xmlDoc;   
                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");   
                xmlDoc.async = false;   
                xmlDoc.load(input.value);   
                console.log(xmlDoc.xml);   
            }   
            //支持FF  
            else if (document.implementation && document.implementation.createDocument) {   
                var xmlDoc;   
                xmlDoc = document.implementation.createDocument("", "", null);   
                xmlDoc.async = false;   
                xmlDoc.load(input.value);   
                console.log(xmlDoc.xml);  
            } else {   
                alert(‘error‘);   
            }   
        }  
////////////////////////////////////////////////////////////
<input type="file" onchange="abc(this)" />
abc=(file)=>{
       
        var reader = new FileReader();  
        reader.readAsText(file.files[0])
        console.log(reader)
    }

 《存入》

function download(filename, text) {
        var a = document.createElement(‘a‘);
        const href = `data:text/plain;charset=utf-8,${encodeURIComponent(text)}`
        a.setAttribute(‘download‘, filename);
        a.setAttribute(‘href‘, href);
        a.click();
    }
    download(‘hello.txt‘, ‘hello world‘);

读存文件

原文:https://www.cnblogs.com/buwngchuxin/p/12718844.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!