Created
June 3, 2013 14:22
-
-
Save davidecassenti/5698500 to your computer and use it in GitHub Desktop.
Node ACS File Upload example This example shows how to upload a file to Node ACS, and how to save it server side. The example uses FormData. Note: upload.html must be on a server (it won't work if running locally as a file); also, it requires jquery to be in the same directory to work.
All other files are server side, in the Node ACS app.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// initialize app | |
function start(app, express) { | |
app.use(express.methodOverride()); | |
// ## CORS middleware | |
// see: http://stackoverflow.com/questions/7067966/how-to-allow-cors-in-express-nodejs | |
// | |
// PLEASE NOTE: ANY SERVER CAN UPLOAD HERE! YOU SHOULS ONLY ALLOW SPECIFIC SERVERS BY | |
// EDITING THE ACCESS-CONTROL-ALLOW-ORIGIN HEADER BELOW | |
var allowCrossDomain = function(req, res, next) { | |
res.header('Access-Control-Allow-Origin', '*'); | |
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE, OPTIONS'); | |
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, x-requested-with'); | |
res.header('Access-Control-Allow-Credentials', 'true'); | |
// intercept OPTIONS method | |
if ('OPTIONS' == req.method) { | |
res.writeHead(200, { | |
'Access-Control-Allow-Origin': '*', | |
'Access-Control-Allow-Methods': 'POST, GET, PUT, DELETE, OPTIONS', | |
'Access-Control-Allow-Headers': 'Authorization, x-requested-with, Content-Type', | |
'Access-Control-Allow-Credentials': 'true' | |
}); | |
res.end(); | |
} else { | |
next(); | |
} | |
}; | |
app.use(allowCrossDomain); | |
} | |
// release resources | |
function stop() { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ACS = require('acs').ACS; | |
ACS.init('YOUR_ACS_KEY', 'YOUR_ACS_SECRET'); | |
function index(req, res) { | |
var file = req.files.file.path; | |
var name = req.files.file.name; | |
ACS.Users.login({ | |
login: 'YOUR_USER_LOGIN', | |
password: 'YOUR_USER_PASSWORD' | |
}, function(e) { | |
if (e.success) { | |
var session_id = e.meta.session_id; | |
ACS.Files.create({ | |
name : name, | |
file : file, | |
session_id: session_id | |
}, function(e) { | |
if (e.success) { | |
res.end('File created'); | |
} else { | |
console.log(e); | |
res.end('Something went wrong'); | |
} | |
}); | |
} else { | |
console.log(e); | |
res.end('Login failed'); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"routes": | |
[ | |
{ "path": "/", "callback": "application#index", "method": "post" } | |
], | |
"filters": | |
[ | |
{ "path": "/", "callback": "" } | |
], | |
"websockets": | |
[ | |
{ "event": "", "callback": ""} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script src="jquery.min.js"></script> | |
<script> | |
function upload() { | |
var formData = new FormData(); | |
formData.append('file', $('#file')[0].files[0]); | |
$.ajax({ | |
url: 'YOUR_NODE_ACS_SERVER_URL', | |
data: formData, | |
cache: false, | |
contentType: false, | |
processData: false, | |
type: 'POST', | |
success: function (data) { | |
alert(data); | |
}, | |
error: function (response, status, error) { | |
console.log('Something went wrong'); | |
} | |
}); | |
return false; | |
} | |
</script> | |
</head> | |
<body> | |
<form id="frm" onsubmit="return false;"> | |
<input type="file" id="file" /> | |
<button onclick="return upload()">Upload</button> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment