Rumah > hujung hadapan web > tutorial js > 常用的js读写文件排序详解

常用的js读写文件排序详解

小云云
Lepaskan: 2018-03-17 16:45:24
asal
1659 orang telah melayarinya

最近写js发现很多规则跟自己想的不一样,毕竟刚上手不就,所以绕了很多弯弯,这里总结记录下,本文主要和大家分享常用的js读写文件排序详解,希望能帮助到大家。

1,由于file标签上传时:onchange方法在再次上传相同文件不在被触发
想要被触发的方法:

   $("#file").on("change", function (evt) {
        var fileExtend = fileName.substring(file.value.lastIndexOf('.'));
        //获取文件后缀  .zip/.png为了方便比较还可以把她转为小写.....
        this.value = '';
        //为了能持续触发onchange 因为当value值相同就不会再触发 特别是点击返回按钮过来的情况
    });
Salin selepas log masuk

2,读取file上传的文件信息

$("#file").on("change", function () {
        var files = $(this).prop('files');        var reader = new FileReader();
        reader.readAsText(files[0], "UTF-8");//读取文件
        reader.onload = function (evt) {
            var fileString = evt.target.result; // 读取文件内容  
    }
Salin selepas log masuk
Salin selepas log masuk

3,生成本地文件

<a onfocus="this.blur();" style="display: none" id="createInvoteBtn" class="ipt-todo" href="javascript:void(0)">生成并导出Txt文件</a>
<a onfocus="this.blur();" style="display: none" id="createInvote" class="ipt-todo hide">code</a>

为了兼容IE请把上述标签写上,由于display:none所以不影响网页内容/**
*fileName  文件名
*fileData 需要写入文件的内容
*/
     function generateFile(fileName,fileData){          var isIE = (navigator.userAgent.indexOf(&#39;MSIE&#39;) >= 0);//是否是IE浏览器
            if (isIE) {                var winSave = window.open();
                winSave.document.open("text", "utf-8");
                winSave.document.write(fileData);
                winSave.document.execCommand("SaveAs", true, fileName);
                winSave.close();
            } else {                var mimeType = &#39;text/plain&#39;;
                $(&#39;#createInvote&#39;).attr(&#39;href&#39;, &#39;data:&#39; + mimeType + &#39;;charset=utf-8,&#39; + encodeURIComponent(fileData));                var btn = document.getElementById(&#39;createInvote&#39;);
                btn.download = fileName;
                document.getElementById(&#39;createInvote&#39;).click();//为了触发createInvote
            }
        }
Salin selepas log masuk
Salin selepas log masuk

4,根据map(key,value)的value重大到小递归排序map

/**
*map 是需要排序的对象
*newMap 排序后的map对象
*/

  var newMap = {};
 function orderMapByvalue(map) {        if (JSON.stringify(map) == "{}") {        //doSomething
            return newMap;//注意for(key in map)是异步遍历,所以对newMap的操作最好在return前
        }        for (key1 in map) {//只是为了获得第一组的key value 遍历一次就会break
            var tempKey = key1;            var tempValue = map[key1];            for (key2 in map) {                if (map[key2] - tempValue >= 0) {//这里是排序规则 根据需求改变
                    tempKey = key2;//接受value最大的key
                    tempValue = map[tempKey];//最大的value
                }
            }
           newMap[tempKey] =tempValue;//注意如果key是数字无论是你是什么时候插入 1还是"1" 系统会自动根据数字的大小重新排序,我这里的key不是纯数字,所以没问题
            delete map[tempKey];            break;//break是为了只for一次,毕竟js我没找到直接获取map第一个元素对的方法,只能这样来获取map里的第一组数据
        }
        orderMapByvalue(map);
    }
Salin selepas log masuk
Salin selepas log masuk

最近写js发现很多规则跟自己想的不一样,毕竟刚上手不就,所以绕了很多弯弯,这里总结记录下

1,由于file标签上传时:onchange方法在再次上传相同文件不在被触发
想要被触发的方法:

   $("#file").on("change", function (evt) {
        var fileExtend = fileName.substring(file.value.lastIndexOf(&#39;.&#39;));//获取文件后缀  .zip/.png为了方便比较还可以把她转为小写.....
        this.value = &#39;&#39;;//为了能持续触发onchange 因为当value值相同就不会再触发 特别是点击返回按钮过来的情况
    });
Salin selepas log masuk

2,读取file上传的文件信息

$("#file").on("change", function () {
        var files = $(this).prop(&#39;files&#39;);        var reader = new FileReader();
        reader.readAsText(files[0], "UTF-8");//读取文件
        reader.onload = function (evt) {
            var fileString = evt.target.result; // 读取文件内容  
    }
Salin selepas log masuk
Salin selepas log masuk

3,生成本地文件

<a onfocus="this.blur();" style="display: none" id="createInvoteBtn" class="ipt-todo" href="javascript:void(0)">生成并导出Txt文件</a>
<a onfocus="this.blur();" style="display: none" id="createInvote" class="ipt-todo hide">code</a>

为了兼容IE请把上述标签写上,由于display:none所以不影响网页内容/**
*fileName  文件名
*fileData 需要写入文件的内容
*/
     function generateFile(fileName,fileData){          var isIE = (navigator.userAgent.indexOf(&#39;MSIE&#39;) >= 0);//是否是IE浏览器
            if (isIE) {                var winSave = window.open();
                winSave.document.open("text", "utf-8");
                winSave.document.write(fileData);
                winSave.document.execCommand("SaveAs", true, fileName);
                winSave.close();
            } else {                var mimeType = &#39;text/plain&#39;;
                $(&#39;#createInvote&#39;).attr(&#39;href&#39;, &#39;data:&#39; + mimeType + &#39;;charset=utf-8,&#39; + encodeURIComponent(fileData));                var btn = document.getElementById(&#39;createInvote&#39;);
                btn.download = fileName;
                document.getElementById(&#39;createInvote&#39;).click();//为了触发createInvote
            }
        }
Salin selepas log masuk
Salin selepas log masuk

4,根据map(key,value)的value重大到小递归排序map

/**
*map 是需要排序的对象
*newMap 排序后的map对象
*/

  var newMap = {};
 function orderMapByvalue(map) {        if (JSON.stringify(map) == "{}") {        //doSomething
            return newMap;//注意for(key in map)是异步遍历,所以对newMap的操作最好在return前
        }        for (key1 in map) {//只是为了获得第一组的key value 遍历一次就会break
            var tempKey = key1;            var tempValue = map[key1];            for (key2 in map) {                if (map[key2] - tempValue >= 0) {//这里是排序规则 根据需求改变
                    tempKey = key2;//接受value最大的key
                    tempValue = map[tempKey];//最大的value
                }
            }
           newMap[tempKey] =tempValue;//注意如果key是数字无论是你是什么时候插入 1还是"1" 系统会自动根据数字的大小重新排序,我这里的key不是纯数字,所以没问题
            delete map[tempKey];            break;//break是为了只for一次,毕竟js我没找到直接获取map第一个元素对的方法,只能这样来获取map里的第一组数据
        }
        orderMapByvalue(map);
    }
Salin selepas log masuk
Salin selepas log masuk

相关推荐:

PHP并发读写文件如何解决

PHP读写文件代码

php 读写文件的实现代码

Atas ialah kandungan terperinci 常用的js读写文件排序详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan