最近写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,生成本地文件生成并导出Txt文件
为了兼容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,生成本地文件生成并导出Txt文件
为了兼容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);
}
相关推荐: