Skip to content

Commit 5040b9a

Browse files
committed
next
1 parent d3798b2 commit 5040b9a

File tree

3 files changed

+75
-64
lines changed

3 files changed

+75
-64
lines changed

examples/webcam.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ <h3>Server</h3>
116116
FileAPI.upload({
117117
url: '/upload'
118118
, files: { shot: file }
119+
, data: {
120+
x: 1,
121+
arr: [1, {y: 1}],
122+
y: "xxxxxx"
123+
}
119124
, complete: function (err, xhr){
120125
var res = JSON.parse(xhr.responseText);
121126
var img = new Image;

node/file-api.js

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
11

22
function convertToBase64(buffer, mimetype) {
3-
return 'data:' + mimetype + ';base64,' + buffer.toString('base64');
3+
return 'data:' + mimetype + ';base64,' + buffer.toString('base64');
44
}
55

66
function fileApi() {
7-
return function (req, res, next) {
8-
req.body = {};
9-
req.files = {};
10-
req.images = {};
11-
12-
req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
13-
var buffersArray = [];
14-
15-
file.on('data', function (data) {
16-
buffersArray.push(data);
17-
});
18-
19-
file.on('end', function () {
20-
var bufferResult = Buffer.concat(buffersArray);
21-
var fileObj = {
22-
dataURL: convertToBase64(bufferResult, mimetype),
23-
mime: mimetype,
24-
size: bufferResult.length
25-
};
26-
req.files[fieldname] = fileObj;
27-
if (mimetype.indexOf('image/') === 0) {
28-
req.images[fieldname] = fileObj;
29-
}
30-
});
31-
});
32-
33-
req.busboy.on('field', function (key, value, keyTruncated, valueTruncated) {
34-
req.body[key] = value;
35-
});
36-
37-
req.busboy.on('finish', function () {
38-
next();
39-
});
40-
};
7+
return function (req, res, next) {
8+
req.body = {};
9+
req.files = {};
10+
req.images = {};
11+
12+
req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
13+
var buffersArray = [];
14+
15+
file.on('data', function (data) {
16+
buffersArray.push(data);
17+
});
18+
19+
file.on('end', function () {
20+
var bufferResult = Buffer.concat(buffersArray);
21+
var fileObj = {
22+
dataURL: convertToBase64(bufferResult, mimetype),
23+
mime: mimetype,
24+
size: bufferResult.length
25+
};
26+
req.files[fieldname] = fileObj;
27+
if (mimetype.indexOf('image/') === 0) {
28+
req.images[fieldname] = fileObj;
29+
}
30+
});
31+
});
32+
33+
req.busboy.on('field', function (key, value, keyTruncated, valueTruncated) {
34+
req.body[key] = value;
35+
console.log(arguments);
36+
});
37+
38+
req.busboy.on('finish', function () {
39+
next();
40+
});
41+
};
4142
}
4243

4344
module.exports = fileApi;

node/server.js

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,50 @@ var app = express();
66
app.use(express.static('.', {index: 'index.html'}));
77

88
app.use(function (req, res, next) {
9-
// Enable CORS for non static files
10-
var origin = req.get('Origin');
11-
12-
if (origin) {
13-
res.set({
14-
'Access-Control-Allow-Origin': origin,
15-
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
16-
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Range, Content-Disposition, Content-Type, X-Foo, X-Rnd',
17-
'Access-Control-Allow-Credentials': 'true'
18-
});
19-
}
20-
next();
9+
// Enable CORS for non static files
10+
var origin = req.get('Origin');
11+
12+
if (origin) {
13+
res.set({
14+
'Access-Control-Allow-Origin': origin,
15+
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
16+
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Range, Content-Disposition, Content-Type, X-Foo, X-Rnd',
17+
'Access-Control-Allow-Credentials': 'true'
18+
});
19+
}
20+
next();
2121
});
2222

2323
var uploadPath = '/upload';
2424

2525
app.options(uploadPath, function (req, res) {
26-
res.end();
26+
res.end();
2727
});
2828

29-
app.post(uploadPath, busboy({immediate: true}), fileApi(), function (req, res) {
30-
var jsonp = req.query.callback || null;
31-
32-
res[jsonp ? 'jsonp' : 'json']({
33-
status: 200,
34-
statusText: 'OK',
35-
images: req.images,
36-
data: {
37-
HEADERS: req.headers,
38-
_REQUEST: req.body,
39-
_FILES: req.files
29+
app.post(
30+
uploadPath,
31+
busboy({immediate: true}), // parse post data
32+
fileApi(),// prepare req.body, req.files and req.images
33+
function (req, res) {
34+
var jsonp = req.query.callback || null;
35+
36+
res[jsonp ? 'jsonp' : 'json']({
37+
status: 200,
38+
statusText: 'OK',
39+
images: req.images,
40+
data: {
41+
HEADERS: req.headers,
42+
_REQUEST: req.body,
43+
_FILES: req.files
44+
}
45+
});
4046
}
41-
});
42-
});
47+
);
4348

4449
var server = app.listen(8000, function () {
4550

46-
var host = server.address().address;
47-
var port = server.address().port;
51+
var host = server.address().address;
52+
var port = server.address().port;
4853

49-
console.log('Test server listening at http://%s:%s', host, port)
54+
console.log('Test server listening at http://%s:%s', host, port)
5055
});

0 commit comments

Comments
 (0)