Home >Web Front-end >JS Tutorial >Detailed explanation of commonly used js reading and writing file sorting

Detailed explanation of commonly used js reading and writing file sorting

小云云
小云云Original
2018-03-17 16:45:241614browse

Recently when writing js, I found that many rules are different from what I thought. After all, I just got started, so I took a lot of detours. Here is a summary and record. This article mainly shares with you the commonly used js reading and writing file sorting details. I hope it can Help everyone.

1, when the file tag is uploaded: the onchange method is no longer triggered when the same file is uploaded again
Method that wants to be triggered:

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

2, read the file information uploaded by 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; // 读取文件内容  
    }

3, generate local files

<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
            }
        }

4, sort map recursively according to the value of map(key, value) from major to small

/**
*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);
    }

           

Recent When writing js, I found that many rules are different from what I thought. After all, I just got started, so I took a lot of detours. Here is a summary and record

1. When uploading with the file tag: the onchange method uploads the same file again. No longer being triggered
Methods that want to be triggered:

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

2, read the file information uploaded by 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; // 读取文件内容  
    }

3, generate local files

<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
            }
        }

4, Recursively sort map according to the value of map(key, value) from major to small

/**
*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);
    }

Related recommendations:

How to solve concurrent reading and writing of files in PHP

PHP read and write file code

php read and write file implementation code

The above is the detailed content of Detailed explanation of commonly used js reading and writing file sorting. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn