ITeye博客

js JavaScript 读取文件数据、导出文本到文件。console输出到本地文件

fantaxy025025 · 2021-03-11 19:41 · js_css_html_xml_nodejs · Web前端 · Markdown

=

需求:输出点内容到本地文件。

方法:下面。

大概原理看看js内容就知晓了。

=

 

from:https://blog.csdn.net/ChisameMuko/article/details/103571861

读取文件数据并输出内容:

(需配合input[type="file"] 使用):

语法:

importRaw(outEle, inEle, type, code)

参数:

<outEle> 输出的dom元素对象或具体值(必要):

  • 具体值:以#开头的ID或以.开头的class。
  • 类如 document.getElementById() document.querySelector() :获取输出元素的dom并传递值。

<inEle> 输入的dom元素对象或具体值或event对象:

  • this:指代此dom元素对象。
  • event:选择元素对象中改变后的file值。
  • 具体值:以#开头的ID或以.开头的class。
  • 类如 document.getElementById() document.querySelector() :获取输入元素的dom并传递值。

<type> 输出方式:

  • 空 | "value":为对象赋值。

  • "html":输出HTML代码。

  • "text":输出文本。

    <type>为编码的值且<code>不存在时, 原<type>将克隆为<code>的功能, 其它不变

<code> 输入文本的编码:

  • 空:文件原编码(未定义时可能会出现乱码)
  • 如"utf8","gb2312"的编码("gb2312"是以汉字为主的编码)

函数:

 

function importRaw(outEle, inEle, type, code) {
    var file, ef = event.target.files || window.event.target.files, reader = new FileReader();
    if (inEle && typeof inEle == "object") {
        if (inEle.files) { file = inEle.files[0]}
        else if (inEle.length > 0 && inEle[0].files) { file = inEle[0].files[0] }
        else if (ef) { file = ef[0] }
    } else if (document.querySelector(inEle) && document.querySelector(inEle).files) { file = document.querySelector(inEle).files[0] }
    else if (ef) { file = ef[0] } else { return }
    if (!code && type != "value" && type != "text" && type != "html") { code = type; type = "value" }
    reader.onload = function() {
        if (reader.result) {
            if (typeof outEle == "string") {
                if (document.querySelector(outEle)) { outEle = document.querySelector(outEle) } else { return }
            } else if (outEle.length > 0) {
                outEle = outEle[0];
            }
            if (type == "text") { outEle.innerText = reader.result }
            else if (type == "html") { outEle.innerHTML = reader.result }
            else if (type == "value") { outEle.value = reader.result }
        }
    }
    reader.readAsText(file, code);
}

 

例子:

<input id="input1" type="file" accept="text/*" onchange="importRaw('#demo1', this, 'value')" />
<textarea id="demo1"></textarea>

<input id="input2" type="file" accept="text/*" onchange="importRaw(document.querySelector('#demo2'), document.getElementById('input2'), 'text', 'gb2312')" />
<p id="demo2"></p>

<script>
	function importRaw(outEle, inEle, type, code) {
		// ...
	}
</script>

 

 

导出字符串文本到文件:

语法:

exportRaw(name, data)

参数:

<name> 导出的文件名

<data> 导出的字符串文本

函数:

 

function exportRaw(name, data) {
	var urlObject = window.URL || window.webkitURL || window;
	var exportBlob = new Blob([data]);
	var e = document.createEvent("MouseEvents");
	var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a");
	save_link.href = urlObject.createObjectURL(exportBlob);
	save_link.download = name;
	e.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
	save_link.dispatchEvent(e);
}

 

 例子:

var text = "This is demo text.";
exportRaw("text_name.txt", text);

 

 

 

=

=

=

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。