deno.land / x / sheetjs@v0.18.3 / demos / function / AzureHTTPTrigger / index.js

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com *//* eslint-env node */// base64 sheetjs.xlsb | curl -F "data=@-;filename=test.xlsb" http://localhost:7262/api/AzureHTTPTrigger
const XLSX = require('xlsx');const formidable = require('formidable');const Readable = require('stream').Readable;var fs = require('fs');
/* formidable expects the request object to be a stream */const streamify = (req) => { if(typeof req.on !== 'undefined') return req; const s = new Readable(); s._read = ()=>{}; s.push(new Buffer(req.body)); s.push(null); Object.assign(s, req); return s;};
module.exports = (context, req) => { const form = new formidable.IncomingForm(); form.parse(streamify(req), (err, fields, files) => { /* grab the first file */ var f = Object.values(files)[0]; if(!f) { context.res = { status: 400, body: "Must submit a file for processing!" }; } else { /* file is stored in a temp directory, so we can point to that and read it */ const wb = XLSX.read(f.path, {type:"file"});
/* convert to specified output type -- default CSV */ const ext = (fields.bookType || "csv").toLowerCase(); const out = XLSX.write(wb, {type:"string", bookType:ext});
context.res = { status: 200, headers: { "Content-Disposition": `attachment; filename="download.${ext}";` }, body: out }; } context.done(); });};
sheetjs

Version Info

Tagged at
2 years ago