Skip to content
Browse files

Send redirect header if a redirect field is present - Fixes #1165.

  • Loading branch information...
1 parent 47bdcea commit 309d1ba7d239b17dcc439039b8656cb0e3fa10ff @blueimp blueimp committed Apr 11, 2012
Showing with 25 additions and 8 deletions.
  1. +1 −1 server/node/package.json
  2. +24 −7 server/node/server.js
View
2 server/node/package.json
@@ -1,6 +1,6 @@
{
"name": "blueimp-file-upload-node",
- "version": "1.0.0",
+ "version": "1.0.1",
"title": "jQuery File Upload Node.js example",
"description": "Node.js implementation example of a file upload handler for jQuery File Upload.",
"keywords": [
View
31 server/node/server.js
@@ -1,6 +1,6 @@
#!/usr/bin/env node
/*
- * jQuery File Upload Plugin Node.js Example 1.0
+ * jQuery File Upload Plugin Node.js Example 1.0.1
* https://github.com/blueimp/jQuery-File-Upload
*
* Copyright 2012, Sebastian Tschan
@@ -82,11 +82,23 @@
'Access-Control-Allow-Methods',
options.accessControl.allowMethods
);
- var handleResult = function (result) {
- var contentType = req.headers.accept.indexOf('application/json') !== -1 ?
- 'application/json' : 'text/plain';
- res.writeHead(200, {'Content-Type': contentType});
- res.end(JSON.stringify(result));
+ var handleResult = function (result, redirect) {
+ if (redirect) {
+ res.writeHead(302, {
+ 'Location': redirect.replace(
+ /%s/,
+ encodeURIComponent(JSON.stringify(result))
+ )
+ });
+ res.end();
+ } else {
+ res.writeHead(200, {
+ 'Content-Type': req.headers.accept
+ .indexOf('application/json') !== -1 ?
+ 'application/json' : 'text/plain'
+ });
+ res.end(JSON.stringify(result));
+ }
},
setNoCacheHeaders = function () {
res.setHeader('Pragma', 'no-cache');
@@ -197,13 +209,14 @@
files = [],
map = {},
counter = 1,
+ redirect,
finish = function () {
counter -= 1;
if (!counter) {
files.forEach(function (fileInfo) {
fileInfo.initUrls(handler.req);
});
- handler.callback(files);
+ handler.callback(files, redirect);
}
};
form.uploadDir = options.tmpDir;
@@ -213,6 +226,10 @@
fileInfo.safeName();
map[path.basename(file.path)] = fileInfo;
files.push(fileInfo);
+ }).on('field', function (name, value) {
+ if (name === 'redirect') {
+ redirect = value;
+ }
}).on('file', function (name, file) {
var fileInfo = map[path.basename(file.path)];
fileInfo.size = file.size;

0 comments on commit 309d1ba

Please sign in to comment.
Something went wrong with that request. Please try again.