您好,欢迎访问一九零五行业门户网

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

最近写js发现很多规则跟自己想的不一样,毕竟刚上手不就,所以绕了很多弯弯,这里总结记录下,本文主要和大家分享常用的js读写文件排序详解,希望能帮助到大家。
1,由于file标签上传时:onchange方法在再次上传相同文件不在被触发
想要被触发的方法:
$("#file").on("change", function (evt) { var fileextend = filename.substring(file.value.lastindexof('.')); //获取文件后缀 .zip/.png为了方便比较还可以把她转为小写..... this.value = ''; //为了能持续触发onchange 因为当value值相同就不会再触发 特别是点击返回按钮过来的情况 });
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; // 读取文件内容 }
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('msie') >= 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 = 'text/plain'; $('#createinvote').attr('href', 'data:' + mimetype + ';charset=utf-8,' + encodeuricomponent(filedata)); var btn = document.getelementbyid('createinvote'); btn.download = filename; document.getelementbyid('createinvote').click();//为了触发createinvote } }

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

最近写js发现很多规则跟自己想的不一样,毕竟刚上手不就,所以绕了很多弯弯,这里总结记录下
1,由于file标签上传时:onchange方法在再次上传相同文件不在被触发
想要被触发的方法:
$("#file").on("change", function (evt) { var fileextend = filename.substring(file.value.lastindexof('.'));//获取文件后缀 .zip/.png为了方便比较还可以把她转为小写..... this.value = '';//为了能持续触发onchange 因为当value值相同就不会再触发 特别是点击返回按钮过来的情况 });
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; // 读取文件内容 }
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('msie') >= 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 = 'text/plain'; $('#createinvote').attr('href', 'data:' + mimetype + ';charset=utf-8,' + encodeuricomponent(filedata)); var btn = document.getelementbyid('createinvote'); btn.download = filename; document.getelementbyid('createinvote').click();//为了触发createinvote } }

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

相关推荐:
php并发读写文件如何解决
php读写文件代码
php 读写文件的实现代码
以上就是常用的js读写文件排序详解的详细内容。
其它类似信息

推荐信息