Created
February 15, 2015 15:57
-
-
Save CodeLuca/9813de8815cce35ea3bb to your computer and use it in GitHub Desktop.
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="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery.form/3.51/jquery.form.min.js"></script> | |
<title>File upload Node.</title> | |
</head> | |
<body> | |
<form id="uploadForm" | |
enctype="multipart/form-data" | |
action="/api/photo" | |
method="post"> | |
<input type="file" name="userPhoto" /> | |
<input type="submit" value="Upload Image" name="submit"> | |
</form> | |
</body> | |
<script> | |
$(document).ready(function() { | |
$('#uploadForm').submit(function() { | |
$(this).ajaxSubmit({ | |
error: function(xhr) { | |
status('Error: ' + xhr.status); | |
}, | |
success: function(response) { | |
console.log(response); | |
} | |
}); | |
//Very important line, it disable the page refresh. | |
return false; | |
}); | |
}); | |
</script> | |
</html> |
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 express = require("express"); | |
var multer = require('multer'); | |
var app = express(); | |
var done = false; | |
app.use(multer({ dest: './uploads/', | |
rename: function (fieldname, filename) { | |
return filename+Date.now(); | |
}, | |
onFileUploadStart: function (file) { | |
console.log(file.originalname + ' is starting ...') | |
}, | |
onFileUploadComplete: function (file) { | |
console.log(file.fieldname + ' uploaded to ' + file.path) | |
done=true; | |
} | |
})); | |
app.get('/',function(req,res){ | |
res.sendfile("index.html"); | |
}); | |
app.post('/api/photo',function(req,res){ | |
if(done==true){ | |
console.log(req.files); | |
res.end("File uploaded."); | |
} | |
}); | |
app.listen(3000,function(){ | |
console.log("Working on port 3000"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment