diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/filereader/onload | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/filereader/onload')
-rw-r--r-- | files/zh-cn/web/api/filereader/onload/index.html | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/filereader/onload/index.html b/files/zh-cn/web/api/filereader/onload/index.html new file mode 100644 index 0000000000..971ffa3d37 --- /dev/null +++ b/files/zh-cn/web/api/filereader/onload/index.html @@ -0,0 +1,25 @@ +--- +title: FileReader.onload +slug: Web/API/FileReader/onload +tags: + - 文件 +translation_of: Web/API/FileReader/onload +--- +<p>{{APIRef}}</p> + +<p>当 <strong>FileReader </strong>读取文件的方式为<strong> </strong> <a href="/en-US/docs/Web/API/FileReader/readAsArrayBuffer">readAsArrayBuffer</a>, <a href="/en-US/docs/Web/API/FileReader/readAsBinaryString">readAsBinaryString</a>, <a href="/en-US/docs/Web/API/FileReader/readAsDataURL">readAsDataURL</a> 或者 <a href="/en-US/docs/Web/API/FileReader/readAsText">readAsText</a> 的时候,会触发一个 {{event('load')}} 事件。从而可以使用 <strong><code>FileReader.onload</code></strong> 属性对该事件进行处理。</p> + +<h2 id="范例">范例</h2> + +<pre class="brush:js; line-numbers language-js"><code class="language-js">// 一个文件上传的回调 <input type="file" onchange="onChange(event)"> +function onChange(event) { + var file = event.target.files[0]; + var reader = new FileReader(); + reader.onload = function(event) { + // 文件里的文本会在这里被打印出来 + console.log(event.target.result) + }; + + reader.readAsText(file); +} +</code></pre> |