Permalink
Browse files

upload-image

  • Loading branch information...
1 parent 9ffb9aa commit eda5593da6ed14810c70f5fde17e1da38a58b170 william committed Jul 1, 2012
View
@@ -0,0 +1 @@
+node_modules/
View
69 app.js
@@ -0,0 +1,69 @@
+/**
+ * Module dependencies.
+ * uploaded images are stored under 'my_dir'/images/ directory which can be accessed and shared.
+ */
+var my_dir = 'C:/Users/ingrid/node_modules/test/upload-app/public/';
+
+var express = require('express'),
+ routes = require('./routes');
+
+var app = module.exports = express.createServer();
+
+// Configuration
+app.configure(function(){
+ app.set('views', __dirname + '/views');
+ app.set('view engine', 'jade');
+ app.use(express.bodyParser());
+ app.use(express.methodOverride());
+ app.use(app.router);
+ app.use(express.static(__dirname + '/public'));
+});
+
+app.configure('development', function(){
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+});
+
+app.configure('production', function(){
+ app.use(express.errorHandler());
+});
+
+// Routes
+app.get('/', function(req, res){
+ //app.get('/', routes.index); first time load ~/public/index.html
+ var html = require('fs').readFileSync(__dirname+'/public/index.html');
+ res.end(html);
+});
+
+app.post('/api/photos', function(req, res) {
+ console.log(JSON.stringify(req.files));
+
+ var serverPath = '/images/' + req.files.userPhoto.name;
+
+ require('fs').rename(
+ req.files.userPhoto.path,
+ my_dir + serverPath,
+ function(error) {
+ if(error) {
+ res.send({
+ error: 'Ah crap! Something bad happened'
+ });
+ return;
+ }
+ res.send({
+ path: serverPath
+ });
+ }
+ );
+});
+
+app.listen(3000, function(){
+ console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
+});
+
+
+var nowjs = require("now");
+var everyone = nowjs.initialize(app);
+
+everyone.now.distributeMessage = function(message){
+ everyone.now.receiveMessage(message);
+};
View
@@ -0,0 +1,9 @@
+{
+ "name": "application-name"
+ , "version": "0.0.1"
+ , "private": true
+ , "dependencies": {
+ "express": "2.5.8"
+ , "jade": ">= 0.0.1"
+ }
+}
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -0,0 +1,81 @@
+<!DOCTYPE html>
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<title>Node Practical Test - William Jiang - upload Image from local machine</title>
+<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
+<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
+<script src="http://malsup.github.com/jquery.form.js"></script>
+<script src="/nowjs/now.js"></script>
+<script>
+$(document).ready(function() {
+
+ // status('Choose a image file:)');
+ now.receiveMessage = function(message){
+ $(message).addClass('img_space').appendTo('div.display');
+ //$(message).appendTo($('body')).wrap("<span/>");
+ }
+
+ $('#uploadForm').submit(function() {
+ //status('Uploading the image file ...');
+
+ $(this).ajaxSubmit({
+
+ error: function(xhr) {
+ status('Error: ' + xhr.status);
+ },
+
+ success: function(response) {
+
+ if(response.error) {
+ status('Opps, something bad happened');
+ return;
+ }
+
+ var imageUrlOnServer = response.path;
+
+ //status('Success, file uploaded to:' + imageUrlOnServer);
+
+ // var img = $('<img/>').attr({'src':imageUrlOnServer, 'title':imageUrlOnServer.replace(/.*\//, '')});
+ // $(img).appendTo($('body')).wrap("<span/>");
+ var img = '<img src="'+imageUrlOnServer+'" title="'+imageUrlOnServer.replace(/.*\//, '')+'" />';
+
+ // fresh all online pages.
+ now.distributeMessage(img);
+
+ }
+ });
+ // Have to stop the form from submitting and causing a page refresh - don't forget this
+ return false;
+ });
+
+ function status(message) {
+ $('#status').text(message);
+ }
+
+ $('#userPhotoInput').change(function(e) {
+ e.preventDefault();
+ $('#uploadForm').submit();
+ return false;
+ });
+});
+</script>
+</head>
+<body>
+<div class="box">
+ <h2>Image Stream</h2>
+ <hr />
+ <p>In the field, input the full URL to your image. You can use the 'Post Image' button to upload it.</p>
+ <div class="main">
+ <form id="uploadForm"
+ enctype="multipart/form-data"
+ action="/api/photos"
+ method="post">
+ <input type="file" id="userPhotoInput" name="userPhoto" size="50" />
+ </form>
+ <div id="status" />
+ <div class="display"></div>
+ </div>
+</div>
+</body>
+</html>
Oops, something went wrong.

0 comments on commit eda5593

Please sign in to comment.