读:
调用方法
<input type="file" name="file" id="file_1" οnchange="openfile(event)"></input>
函数体:
<script>
function openfile(event)
{
var input = event.target;
var reader = new FileReader();
reader.onload = function(){
var text = reader.result;
config_str = text;
alert(config_str);
};
reader.readAsText(input.files[0]);
};
</scriopt>
写:
<script>
function download(filename, text) {
var pom = document.createElement("a");
pom.setAttribute( "href", "data:text/plain;charset=utf-8," + encodeURIComponent(text));
pom.setAttribute("download", filename);
if (document.createEvent) {
var event = document.createEvent("MouseEvents");
event.initEvent("click", true, true);
pom.dispatchEvent(event);
}
else
{
pom.click();
}
}
download("data.txt","hello world");
</script>
(这是网搜的,出处找不到了,若有人找到了可在评论中补上。–不胜感激(~ _ ~))