Last active
December 11, 2015 05:48
-
-
Save PaulWoodIII/4554198 to your computer and use it in GitHub Desktop.
Trying to get Gridform working but failed miserably: Gridform is here. http://aheckmann.github.com/gridform/ I need a form somewhere to make it easier to test on the browser. need a lot of modules in your project to get this to work in the first place winedb came from nodecellar code
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 http = require('http'); | |
var assert = require('assert'); | |
var gridform = require('gridform'); | |
var formidable = require('formidable'); | |
var mongo = require('mongo') | |
, database = mongo.connect('mydb') | |
, collection = database.collection('fs'); | |
// assuming you've already created a db instance and opened it | |
gridform.db = database; | |
gridform.mongo = mongo; | |
// in your http server | |
var app = http.Server(function (req, res) { | |
// create a gridform | |
var form = gridform(); | |
// returns a custom IncomingForm | |
assert(form instanceof formidable.IncomingForm); | |
// optionally store per-file metadata | |
form.on('fileBegin', function (name, file) { | |
file.metadata = 'so meta' | |
}) | |
// parse normally | |
form.parse(req, function (err, fields, files) { | |
if (err) { | |
res.send(err) | |
} | |
else{ | |
// use files and fields as you do today | |
var file = files.upload; | |
file.name // the uploaded file name | |
file.type // file type per [mime](https://github.com/bentomas/node-mime) | |
file.size // uploaded file size (file length in GridFS) named "size" for compatibility | |
file.path // same as file.name. included for compatibility | |
file.lastModified // included for compatibility | |
// files contain additional gridfs info | |
file.root // the root of the files collection used in MongoDB ('fs' here means the full collection in mongo is named 'fs.files') | |
file.id // the ObjectId for this file | |
} | |
}); | |
}); | |
app.listen(1337, '127.0.0.1'); | |
console.log('Server running at http://127.0.0.1:1337/'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment