Skip to content

Commit 309d1ba

Browse files
committed
Send redirect header if a redirect field is present - Fixes blueimp#1165.
1 parent 47bdcea commit 309d1ba

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

server/node/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "blueimp-file-upload-node",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"title": "jQuery File Upload Node.js example",
55
"description": "Node.js implementation example of a file upload handler for jQuery File Upload.",
66
"keywords": [

server/node/server.js

+24-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22
/*
3-
* jQuery File Upload Plugin Node.js Example 1.0
3+
* jQuery File Upload Plugin Node.js Example 1.0.1
44
* https://github.com/blueimp/jQuery-File-Upload
55
*
66
* Copyright 2012, Sebastian Tschan
@@ -82,11 +82,23 @@
8282
'Access-Control-Allow-Methods',
8383
options.accessControl.allowMethods
8484
);
85-
var handleResult = function (result) {
86-
var contentType = req.headers.accept.indexOf('application/json') !== -1 ?
87-
'application/json' : 'text/plain';
88-
res.writeHead(200, {'Content-Type': contentType});
89-
res.end(JSON.stringify(result));
85+
var handleResult = function (result, redirect) {
86+
if (redirect) {
87+
res.writeHead(302, {
88+
'Location': redirect.replace(
89+
/%s/,
90+
encodeURIComponent(JSON.stringify(result))
91+
)
92+
});
93+
res.end();
94+
} else {
95+
res.writeHead(200, {
96+
'Content-Type': req.headers.accept
97+
.indexOf('application/json') !== -1 ?
98+
'application/json' : 'text/plain'
99+
});
100+
res.end(JSON.stringify(result));
101+
}
90102
},
91103
setNoCacheHeaders = function () {
92104
res.setHeader('Pragma', 'no-cache');
@@ -197,13 +209,14 @@
197209
files = [],
198210
map = {},
199211
counter = 1,
212+
redirect,
200213
finish = function () {
201214
counter -= 1;
202215
if (!counter) {
203216
files.forEach(function (fileInfo) {
204217
fileInfo.initUrls(handler.req);
205218
});
206-
handler.callback(files);
219+
handler.callback(files, redirect);
207220
}
208221
};
209222
form.uploadDir = options.tmpDir;
@@ -213,6 +226,10 @@
213226
fileInfo.safeName();
214227
map[path.basename(file.path)] = fileInfo;
215228
files.push(fileInfo);
229+
}).on('field', function (name, value) {
230+
if (name === 'redirect') {
231+
redirect = value;
232+
}
216233
}).on('file', function (name, file) {
217234
var fileInfo = map[path.basename(file.path)];
218235
fileInfo.size = file.size;

0 commit comments

Comments
 (0)