File uploads with Railway

49 views
Skip to first unread message

andrewsenter

unread,
Aug 5, 2011, 5:33:35 AM8/5/11
to RailwayJS
Couldn't find a native way to handle these, seems multipart enctypes
are stripped. So, if anyone is wondering how to do this, here's a
chunk of code that should help you along your way:

var formidable = require('formidable');

app.post('/moments', function(req, res, next){

var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {

console.log('received upload:\n\n');
console.log(sys.inspect({fields: fields, files: files}));

this.moment = new this.app.models.Moment;
['title', 'duration'].forEach(function (field) {
if (typeof fields[field] !== 'undefined') {
this.moment[field] = fields[field];
}
}.bind(this));

this.moment.save(function (errors) {
if (errors) {
console.log(errors)
return;
} else {
return;
}
}.bind(this));

}.bind(this));

});

Bruno Tavares

unread,
Aug 11, 2011, 7:15:52 AM8/11/11
to RailwayJS
Nice one! It's just not very clear for me if this goes into app.js, or
into "action('create', function () {" inside the given controller. Can
you please inform me that?

On Aug 5, 6:33 am, andrewsenter <andrewsen...@gmail.com> wrote:
> Couldn't find a native way to handle these, seems multipart enctypes
> are stripped. So, if anyone is wondering how to do this, here's a
> chunk of code that should help you along your way:
>
> var formidable = require('formidable');
>
> app.post('/moments', function(req, res, next){
>
>   var form = new formidable.IncomingForm();
>   form.parse(req, function(err, fields, files) {
>
>     console.log('receivedupload:\n\n');

Bruno Tavares

unread,
Aug 11, 2011, 12:54:55 PM8/11/11
to RailwayJS
Never mind, I replace my normal 'create' action for your snippet. The
question now is about returning a view. After saving, you just do a
return;. In normal execution flow it would render a view, define flash
message and so on.

I tried to add:

flash('info', 'Post created');
redirect(path_to.posts);

But I got an error :(

this.request.flash.apply(this.request,
Array.prototype.slice.call(argument
^
TypeError: Cannot read property 'flash' of null

Of course, I didn't used RailWay normal flow... but anyone know how we
can solve that?

Anatoliy C.

unread,
Sep 1, 2011, 9:48:33 AM9/1/11
to RailwayJS
Guys, I've prepared test app demonstrates file uploads in RailwayJS:
https://github.com/anatoliychakkaev/railway-example-upload
The trick is to use sync version of connect-form middleware, you can
found it here: https://github.com/anatoliychakkaev/connect-form-sync

Why we need syncronous uploading here? Because it simplier, your
controller action should be as simple as:

action('create', function () {
this.file = new File();
var tmpFile = req.form.files.file;
this.file.upload(tmpFile.name, tmpFile.path, function (err) {
if (err) {
console.log(err);
this.title = 'New file';
flash('error', 'File can not be created');
render('new');
} else {
flash('info', 'File created');
redirect(path_to.files);
}
}.bind(this));
});

In other words, your controller gets control only after all files has
been fully uploaded.

But, if you want to implement async file uploading, you should use
regular version of connect-form middleware. Do not forget to switch
off csrf protection in application_controller

Santosh Srinivas

unread,
Jul 16, 2012, 12:29:58 AM7/16/12
to rail...@googlegroups.com
Hi Anatoily,

Does this still work? I see this example is a little dated ... are there any config changes needed?
I am getting the following error: Error: Cannot find module 'connect-form-sync'

but i used npm to install it already.

Thanks,
Santosh

Márcio Rezende

unread,
Jul 16, 2012, 12:45:01 AM7/16/12
to rail...@googlegroups.com
Hey guy a long time i no see node but look to it in google search
nodejs blueimp


Arte e tecnologia
(11) 6300-0907  (Claro)


2012/7/16 Santosh Srinivas <santosh....@gmail.com>

Márcio Rezende

unread,
Jul 16, 2012, 12:57:54 AM7/16/12
to rail...@googlegroups.com
https://github.com/blueimp/jQuery-File-Upload 

Arte e tecnologia
(11) 6300-0907  (Claro)


2012/7/16 Santosh Srinivas <santosh....@gmail.com>
Hi Anatoily,
Reply all
Reply to author
Forward
0 new messages