aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/file/getasdataurl
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:40:17 -0500
commit33058f2b292b3a581333bdfb21b8f671898c5060 (patch)
tree51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/file/getasdataurl
parent8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff)
downloadtranslated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2
translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip
initial commit
Diffstat (limited to 'files/zh-cn/web/api/file/getasdataurl')
-rw-r--r--files/zh-cn/web/api/file/getasdataurl/index.html59
1 files changed, 59 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/file/getasdataurl/index.html b/files/zh-cn/web/api/file/getasdataurl/index.html
new file mode 100644
index 0000000000..7f72b124d0
--- /dev/null
+++ b/files/zh-cn/web/api/file/getasdataurl/index.html
@@ -0,0 +1,59 @@
+---
+title: File.getAsDataURL()
+slug: Web/API/File/getAsDataURL
+translation_of: Web/API/File/getAsDataURL
+---
+<div>{{APIRef("File API") }}</div>
+
+<p>{{non-standard_header}}</p>
+
+<p>{{deprecated_header(7.0)}}</p>
+
+<h2 id="概述">概述</h2>
+
+<p>getAsDataURL<code>函数返回一个形如 </code><a href="/en-US/docs/data_URIs"><code>data:</code></a> 的 URL,这个URL包含了所涉及到的内容的编码形式。</p>
+
+<div class="note">
+<p><strong>注:</strong> 这个方法已经废弃,你应该使用 {{domxref("FileReader")}} 对象中的{{domxref("FileReader.readAsDataURL","readAsDataURL()")}} 方法作为替代。</p>
+</div>
+
+<h2 id="语法">语法</h2>
+
+<pre>var url = <em>instanceOfFile</em>.getAsDataURL();</pre>
+
+<h3 id="返回值">返回值</h3>
+
+<p>一个形如 <a href="/en-US/docs/data_URIs"><code>data:</code></a> 的URL字符串</p>
+
+<h2 id="范例">范例</h2>
+
+<pre class="brush: js">// fileInput 是一个 HTMLInputElement 元素: &lt;input type="file" id="myfileinput" multiple&gt;
+var fileInput = document.getElementById("myfileinput");
+
+// files 是一个 FileList 对象(类似 NodeList 对象)
+var files = fileInput.files;
+
+// 允许的文件格式数组
+var accept = ["image/png"];
+
+// img 是一个 HTMLImgElement 元素: &lt;img id="myimg"&gt;
+var img = document.getElementById("myimg");
+
+// 假设我们接收第一个所选中的文件类型
+if (accept.indexOf(files[0].mediaType) &gt; -1) {
+ // 显示图片
+ // 和 &lt;img src="data:image/png,&lt;imagedata&gt;"&gt; 效果一样
+ img.src = files[0].getAsDataURL();
+}
+</pre>
+
+<h2 id="详细说明">详细说明</h2>
+
+<p>没有其他说明</p>
+
+<h2 id="参考文章">参考文章</h2>
+
+<ul>
+ <li>{{domxref("File")}}</li>
+ <li>{{domxref("FileReader")}}</li>
+</ul>