forked from SheetJS/sheetjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodejs.js
84 lines (79 loc) · 2.41 KB
/
nodejs.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com */
var http = require('http');
var XLSX = require('xlsx');
var formidable = require('formidable');
var html = "";
var PORT = 3000;
var extmap = {};
var server = http.createServer(function(req, res) {
if(req.method !== 'POST') return res.end(html);
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
var f = files[Object.keys(files)[0]];
var wb = XLSX.readFile(f.path);
var ext = (fields.bookType || "xlsx").toLowerCase();
res.setHeader('Content-Disposition', 'attachment; filename="download.' + (extmap[ext] || ext) + '";');
res.end(XLSX.write(wb, {type:"buffer", bookType:ext}));
});
}).listen(PORT);
html = [
'<pre>',
'<h3><a href="http://sheetjs.com/">SheetJS File Converter</a></h3>',
'Upload a file to convert the contents to another format.',
'',
'<b>Form Fields</b>:',
'- bookType: output format type (defaults to "XLSX")',
'- basename: basename for output file (defaults to "download")',
'',
'<form method="POST" enctype="multipart/form-data" action="/">',
'<input type="file" id="file" name="file"/>',
'<select name="bookType">',
[
["xlsb", "XLSB"],
["xlsx", "XLSX"],
["xlsm", "XLSM"],
["biff8", "BIFF8 XLS"],
["biff5", "BIFF5 XLS"],
["biff2", "BIFF2 XLS"],
["xlml", "SSML 2003"],
["ods", "ODS"],
["fods", "Flat ODS"],
["csv", "CSV"],
["txt", "Unicode Text"],
["sylk", "Symbolic Link"],
["html", "HTML"],
["dif", "DIF"],
["dbf", "DBF"],
["rtf", "RTF"],
["prn", "Lotus PRN"],
["eth", "Ethercalc"],
].map(function(x) { return ' <option value="' + x[0] + '">' + x[1] + '</option>'; }).join("\n"),
'</select>',
'<input type="submit" value="Submit Form">',
'</form>',
'',
'<b>Form code:</b>',
'<form method="POST" enctype="multipart/form-data" action="/">',
'<input type="file" id="file" name="file"/>',
'<select name="bookType">',
'<!-- options here -->',
'</select>',
'<input type="submit" value="Submit Form">',
'</form>',
'',
'<b>fetch Code:</b>',
'var blob = new Blob("1,2,3\\n4,5,6".split("")); // original file',
'var fd = new FormData();',
'fd.set("data", blob, "foo.bar");',
'fd.set("bookType", "xlsb");',
'var res = await fetch("/", {method:"POST", body:fd});',
'var data = await res.arrayBuffer();',
'</pre>'
].join("\n");
extmap = {
"biff2" : "xls",
"biff5" : "xls",
"biff8" : "xls",
"xlml" : "xls"
};
console.log('listening on port ' + PORT);