From d01e10c3f21482c87a42b56dc02e215f9ee78c30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20W=C3=A4rting?= Date: Wed, 22 Aug 2018 09:00:00 +0200 Subject: [PATCH] update XHR (fetch) demo --- demos/xhr/README.md | 23 ++++++++--------------- demos/xhr/fetch.html | 14 +++++--------- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/demos/xhr/README.md b/demos/xhr/README.md index a8e3594..8056833 100644 --- a/demos/xhr/README.md +++ b/demos/xhr/README.md @@ -116,27 +116,20 @@ axios("/upload", {method: "POST", data: fd}); ## fetch -For downloading data, `response.blob()` resolves to a `Blob` object that can be -converted to `ArrayBuffer` using a `FileReader`: +For downloading data, `response.arrayBuffer()` resolves to an `ArrayBuffer` that +can be converted to `Uint8Array` and passed to `XLSX.read`: ```js fetch(url).then(function(res) { /* get the data as a Blob */ if(!res.ok) throw new Error("fetch failed"); - return res.blob(); -}).catch(function(err) { - /* error in getting data */ -}).then(function(blob) { - /* configure a FileReader to process the blob */ - var reader = new FileReader(); - reader.addEventListener("loadend", function() { - /* parse the data when it is received */ - var data = new Uint8Array(this.result); - var workbook = XLSX.read(data, {type:"array"}); + return res.arrayBuffer(); +}).then(function(ab) { + /* parse the data when it is received */ + var data = new Uint8Array(ab); + var workbook = XLSX.read(data, {type:"array"}); - /* DO SOMETHING WITH workbook HERE */ - }); - reader.readAsArrayBuffer(blob); + /* DO SOMETHING WITH workbook HERE */ }); ``` diff --git a/demos/xhr/fetch.html b/demos/xhr/fetch.html index b0a6d10..a85a7da 100644 --- a/demos/xhr/fetch.html +++ b/demos/xhr/fetch.html @@ -41,15 +41,11 @@ document.getElementById('outfile').innerHTML = '