aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/body/arraybuffer
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/body/arraybuffer
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/body/arraybuffer')
-rw-r--r--files/zh-cn/web/api/body/arraybuffer/index.html142
1 files changed, 142 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/body/arraybuffer/index.html b/files/zh-cn/web/api/body/arraybuffer/index.html
new file mode 100644
index 0000000000..3cf99f2e58
--- /dev/null
+++ b/files/zh-cn/web/api/body/arraybuffer/index.html
@@ -0,0 +1,142 @@
+---
+title: Body.arrayBuffer()
+slug: Web/API/Body/arrayBuffer
+translation_of: Web/API/Body/arrayBuffer
+---
+<p>{{APIRef("Fetch")}}{{ SeeCompatTable() }}</p>
+
+<p> {{domxref("Body")}}上的<strong><code>方法 arrayBuffer()</code></strong> 接受一个 {{domxref("Response")}} 流, 并等待其读取完成. 它返回一个 promise 实例, 并 resolve 一个 {{domxref("ArrayBuffer")}} 对象.</p>
+
+<h2 id="语法">语法</h2>
+
+<pre class="brush: js">response.arrayBuffer().then(function(buffer) {
+ // do something with buffer
+)};</pre>
+
+<h3 id="参数">参数</h3>
+
+<p>无。</p>
+
+<h3 id="返回值">返回值</h3>
+
+<p>A promise that resolves with an {{domxref("ArrayBuffer")}}.</p>
+
+<h2 id="例子">例子</h2>
+
+<p>In our <a href="https://github.com/mdn/fetch-examples/tree/gh-pages/fetch-array-buffer">fetch array buffer example</a> (run <a href="http://mdn.github.io/fetch-examples/fetch-array-buffer/">fetch array buffer live</a>), we have a Play button. When pressed, the <code>getData()</code> function is run.</p>
+
+<p>In <code>getData()</code> we create a new request using the {{domxref("Request.Request")}} constructor, then use it to fetch an OGG music track. We also use {{domxref("AudioContext.createBufferSource")}} to create an audio buffer source.  When the fetch is successful, we read an {{domxref("ArrayBuffer")}} out of the response using <code>arrayBuffer()</code>, decode the audio data using {{domxref("AudioContext.decodeAudioData")}}, set the decoded data as the audio buffer source's buffer (<code>source.buffer</code>), then connect the source up to the {{domxref("AudioContext.destination")}}.</p>
+
+<p>Once <code>getData()</code> has finished running, we start the audio source playing with <code>start(0)</code>, then disable the play button so it can't be clicked again when it is already playing (this would cause an error.)</p>
+
+<pre class="brush: js">function getData() {
+ source = audioCtx.createBufferSource();
+
+ var myRequest = new Request('viper.ogg');
+
+ fetch(myRequest).then(function(response) {
+ response.arrayBuffer().then(function(buffer) {
+ audioCtx.decodeAudioData(buffer, function(decodedData) {
+ source.buffer = decodedData;
+ source.connect(audioCtx.destination);
+ });
+ });
+ });
+};
+
+// wire up buttons to stop and play audio
+
+play.onclick = function() {
+ getData();
+ source.start(0);
+ play.setAttribute('disabled', 'disabled');
+}</pre>
+
+<h2 id="标准">标准</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">标准</th>
+ <th scope="col">状态</th>
+ <th scope="col">备注</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('Fetch','#dom-body-arraybuffer','arrayBuffer()')}}</td>
+ <td>{{Spec2('Fetch')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<p>{{ CompatibilityTable() }}</p>
+
+<div id="compat-desktop">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Chrome</th>
+ <th>Firefox (Gecko)</th>
+ <th>Internet Explorer</th>
+ <th>Opera</th>
+ <th>Safari (WebKit)</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{ CompatChrome(41) }}<sup>[1]</sup><br>
+ {{ CompatChrome(42) }}<br>
+  </td>
+ <td>34<sup>[1]</sup><br>
+ {{ CompatGeckoDesktop(39)}}</td>
+ <td>{{ CompatNo }}</td>
+ <td>
+ <p>28<sup>[1]</sup><br>
+ 29</p>
+ </td>
+ <td>{{ CompatNo }}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<div id="compat-mobile">
+<table class="compat-table">
+ <tbody>
+ <tr>
+ <th>Feature</th>
+ <th>Android</th>
+ <th>Firefox Mobile (Gecko)</th>
+ <th>Firefox OS (Gecko)</th>
+ <th>IE Phone</th>
+ <th>Opera Mobile</th>
+ <th>Safari Mobile</th>
+ <th>Chrome for Android</th>
+ </tr>
+ <tr>
+ <td>Basic support</td>
+ <td>{{ CompatNo }}</td>
+ <td>{{ CompatNo }}</td>
+ <td>{{ CompatNo }}</td>
+ <td>{{ CompatNo }}</td>
+ <td>{{ CompatNo }}</td>
+ <td>{{ CompatNo }}</td>
+ <td>{{ CompatNo }}</td>
+ </tr>
+ </tbody>
+</table>
+</div>
+
+<p>[1] In Chrome 42, Firefox 34 and Opera 28 support for <code>arrayBuffer()</code> was hidden behind a preference.</p>
+
+<h2 id="参考">参考</h2>
+
+<ul>
+ <li><a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker API</a></li>
+ <li><a href="/en-US/docs/Web/HTTP/Access_control_CORS">HTTP access control (CORS)</a></li>
+ <li><a href="/en-US/docs/Web/HTTP">HTTP</a></li>
+</ul>
+
+<div id="sVim-command" style="display: none;">-- NORMAL --</div>