1
forked from sheetjs/sheetjs

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

@ -116,27 +116,20 @@ axios("/upload", {method: "POST", data: fd});
## fetch ## fetch
For downloading data, `response.blob()` resolves to a `Blob` object that can be For downloading data, `response.arrayBuffer()` resolves to an `ArrayBuffer` that
converted to `ArrayBuffer` using a `FileReader`: can be converted to `Uint8Array` and passed to `XLSX.read`:
```js ```js
fetch(url).then(function(res) { fetch(url).then(function(res) {
/* get the data as a Blob */ /* get the data as a Blob */
if(!res.ok) throw new Error("fetch failed"); if(!res.ok) throw new Error("fetch failed");
return res.blob(); return res.arrayBuffer();
}).catch(function(err) { }).then(function(ab) {
/* error in getting data */ /* parse the data when it is received */
}).then(function(blob) { var data = new Uint8Array(ab);
/* configure a FileReader to process the blob */ var workbook = XLSX.read(data, {type:"array"});
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"});
/* DO SOMETHING WITH workbook HERE */ /* DO SOMETHING WITH workbook HERE */
});
reader.readAsArrayBuffer(blob);
}); });
``` ```

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