update XHR (fetch) demo

This commit is contained in:
Jimmy Wärting 2018-08-22 09:00:00 +02:00
parent ab445c8897
commit d01e10c3f2
2 changed files with 13 additions and 24 deletions

View File

@ -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 */
});
```

View File

@ -41,15 +41,11 @@ document.getElementById('outfile').innerHTML = '<a href="' + demo + '.' + book +
fetch(url).then(function(res) {
if(!res.ok) throw new Error("fetch failed");
return res.blob();
}).then(function(blob) {
var reader = new FileReader();
reader.addEventListener("loadend", function() {
var data = new Uint8Array(this.result);
var wb = XLSX.read(data, {type:"array"});
process_wb(wb);
});
reader.readAsArrayBuffer(blob);
return res.arrayBuffer();
}).then(function(ab) {
var data = new Uint8Array(ab);
var wb = XLSX.read(data, {type:"array"});
process_wb(wb);
});
document.getElementById('ulbutton').onclick = function() {