<!doctype html>
<html manifest="/web/log.manifest">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="width:100%">
<div style="margin:auto;width:700;height:auto">
请输入数据:<br>
<textarea id="datas" placeholder="请输入要下载的数据,用逗号分开" style="width:500px;height:500px;">
班级,姓名
4-10,张三
4-10,李四
</textarea>
<a id="download" href="#" download>下载</a>
</div>
</body>
<script>
var download_flag = (function(){
var flag = false;
return {
set:function(f){
flag = f;
},
get:function(){
return flag;
}
}
})();
document.getElementById("download").onclick = function(){
// var data = ['你好,你好,你好\nd,e,f'];
var data = document.getElementById("datas").value;
// var data = 'a,b,c\nd,e,f\n';
console.log(data);
if(download_flag.get()==true){
download_flag.set(false);
return true;
}else{
saveCSV(data); //下载
return false;
}
}
function saveCSV(_data){
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
console.log(requestFileSystem);
window.webkitStorageInfo.requestQuota(window.PERSISTENT, 1024*1024, function(grantedBytes) {
window.requestFileSystem(window.TEMPORARY, 1024*1024, function(fs){
fs.root.getFile('log.csv', {create: true}, function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
var url = fileEntry.toURL();
document.getElementById("download").href = url; //此处把下载代码写入下载的链接中
download_flag.set(true);
document.getElementById("download").click();
};
fileWriter.onerror = function(e) {
//文件创建失败
};
/**************************************************************/
var bolb = new Blob([_data],{type:'text/plain'});
fileWriter.write(bolb);
}, function(){});
}, function(){});
});
}, function(e) {
console.log('Error', e);
});
}
</script>
</html>
遇到了一个问题: 生成的文件总是utf-8格式的, office软件如果文件为utf-8容易乱码, 查了N多资料, 想生成gbk的,但始终不行,求解决方案