Formidable stock during parse

525 views Asked by At

When I'm starting to upload file with my app it stock after first step. Bellow code of upload function

exports.upload = function(req, res){
    var form = formidable.IncomingForm();
    console.log('Upload');
    form.parse(req, function(err, fields, files){
        if(err){
            console.log(err);            
            res.send(err);
            return;
        }
        console.log('Parsing');
        fs.readFile(files.image.path, function(err, data){
            if(err){
                res.send(err);
            }
            var imageName =  files.image.name;
            if(!imageName){
                console.log("Parsing issue - sending 500");
                res.writeHead(500, {'Content-type':'text/plain'});
                res.write('Parsing error');
                res.end();
                return;
            }
            var fullPath = photoDir + imageName;
            var thumbPath = thumbDir + imageName;
            fs.writeFile(fullPath, data, function(err){
                im.resize({
                    srcPath: fullPath,
                    dstPath: thumbPath,
                    width: 100
                },function(err, stdout, stderr){
                    if(err){
                        console.log(err);
                        res.send(err);
                        return;
                    }
                    var newPhoto = new Photo({
                        'fullsize_name':imageName,
                        'thumbsize_name':imageName
                    });
                    newPhoto.save(function(err){
                        if(err){
                            res.send(err);
                        }
                        res.json(newPhoto);
                        console.log(newPhoto);
                    });
                });
            });
        });
    });
}

And server configuration part

var express = require('express');
var logger = require('morgan');
var body_parser = require('body-parser');
var mongoose = require('mongoose');
var passport = require('passport');
var multipart = require('connect-multiparty');
.
.
.
var app = express();
var router = express.Router();
var multipartMiddleware = multipart();
.
.
.
/** PHOTO OPERATIONS **/
router.route('/photos')
    .post(authController.isAuthenticated, photoController.upload);
.
.
.
app.use(passport.initialize());
app.use(logger('dev'));
app.use(multipartMiddleware);
app.use(body_parser.json());
app.use(body_parser.urlencoded({
    extended: true
}));

app.use('/api', router);

Do you have any idea why it is stopping when I'm calling

form.parse(req, function(err, fields, files){

1

There are 1 answers

0
Jainam MJ On

Have you missed { enctype='multipart/form-data' } in form tag in your html file? Adding that might work.