Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

File upload in node.js just keeps uploading indefinitely?

File upload in node.js just keeps uploading indefinitely?

Problem

I'm following this tutorial: http://vimeo.com/17442755. It teaches you how to upload files using node.js.

This is the full code in app.js:

var express = require('express');
var multipart = require('./multipart');
var fs = require('fs');

var app = express.createServer();

app.configure(function() {
    app.use(express.logger());
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.static(__dirname + '/static'));
});

app.configure('development', function () {
    app.use(express.logger());
    app.use(express.errorHandler({
        dumpExceptions: true,
        showStack: true
    }));
});

app.configure('production', function () {
    app.use(express.errorHandler());
});

app.set('views', __dirname + '/views');
app.set('view engine', 'jade');

app.get('/', function(req, res) {
    res.render('root');
});

var products = require('./products');

app.get('/products', function(req, res) {
    res.render('products/index', {locals: {
        products: products.all
    }});
});

app.get('/products/new', function(req, res) {
    res.render('products/new', {locals: {
        product: req.body && req.body.product || products.new
    }});
});

app.post('/products', function(req, res) {
    var id = products.insert(req.body.product);
    res.redirect('/products/' + id);
});

app.get('/products/:id', function(req, res) {
    var product = products.find(req.params.id);
    res.render('products/show', {locals: {
        product: product
    }});
});

app.get('/products/:id/edit', function(req, res) {
    var product = products.find(req.params.id);
    res.render('products/edit', {locals: {
        product: product
    }});
});

app.put('/products/:id', function(req, res) {
    var id = req.params.id;
    products.set(id, req.body.product);
    res.redirect('/products/'+id);
});

/* Photos */

app.get('/photos/new', function(req, res) {
    res.render('photos/new');
});

app.post('/photos', function(req, res) {
    req.setEncoding('binary');

    var parser = multipart.parser();

    parser.headers = req.headers;

    parser.onPartBegin = function(part) {
        ws = fs.createWriteStream(__dirname + '/static/upload/photos/' + part.filename);
        ws.on('error', function(err) {
            throw err;
        });
    };

    parser.onData = function(data) {
        ws.write(data);
    };

    parser.onPartEnd = function() {
        ws.end();
        parser.close();
        res.send('File sucessfully uploaded.');
    };

    req.on('data', function(data) {
        parser.write(data);
    });

});

app.listen(4000);

Now when I try uploading the file, the browser just keep loading forever.

Any suggestions to fix this?

Problem courtesy of: alexchenco

Solution

You are using multipart.js which is broken:

Current State

Pre-pre-alpha. Almost nothing is here, and what is here is likely completely broken.

If you are asking about this, you probably ought to check out Felix's node-formidable library.

As Isaacs (the author of multipart) suggests, you should really use node-formidable instead.

Solution courtesy of: alessioalex

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

File upload in node.js just keeps uploading indefinitely?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×