2011-12-02 18 views

Respuesta

18

Ésta es la forma en que lo hice (echa un vistazo a la .on (archivo' ...) función):

// import required modules 
    var express = require('express'); 
    var formidable = require("formidable"); // used for parsing form data 
    var fs = require('fs'); 

// prevent bodyParser from handling multipart forms (ie only handle get and post requests) 
    delete express.bodyParser.parse['multipart/form-data']; 

// create server 
    var app = express.createServer(); 

// connect middleware 
    app.configure(function() { 
     // creates body object on request object 
      app.use(express.bodyParser()); 
      app.use(express.methodOverride()); 
     // used to display static files such as css 
      app.use(express.static(__dirname + '/static')); 
    }); 

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

//****************************** 
//* create routes 
//****************************** 

// file upload handling 
    app.get('/photos/new', function(request, response) { 
     response.render('photos/new'); 
    }); 
    app.post('/photos', function(request, response) { 
     console.log('in /photos handler'); 
     var form = new formidable.IncomingForm(); 

     form.uploadDir = __dirname + '/static/upload/photos/'; 

     form.on('file', function(field, file) { 
      //rename the incoming file to the file's name 
       fs.rename(file.path, form.uploadDir + "/" + file.name); 
     }); 

     form.on('error', function(err) { 
      console.log("an error has occured with form upload"); 
      console.log(err); 
      request.resume(); 
     }); 

     form.on('aborted', function(err) { 
      console.log("user aborted upload"); 
     }); 

     form.on('end', function() { 
      console.log('-> upload done'); 
     }); 

     form.parse(request, function() { 
       response.render('photos/new'); 
     }); 
    }); 

// start our server 
    app.listen(8888); 
+1

'.on' me daría errores. Lo tengo para trabajar, sin embargo, con 'form.on' – Houseman

1

se puede obtener el nombre original del objeto de archivo formidable:

form.parse(req, function(err, fields, files) { 
    var filename = files.upload.name; 
}); 

espero que esto ayude

+0

Gracias. Entonces, ¿te fs renombra desde aquí? ¿Hay alguna forma para que el analizador de formularios haga esto? –

9

Aquí es mi propia respuesta:

form.uploadDir = "temp/uploads"; 
form.keepExtensions = true; 

form 
    .on('error', function(err) { 
     throw err; 
    }) 

    .on('field', function(field, value) { 
     //receive form fields here 
    }) 

    /* this is where the renaming happens */ 
    .on ('fileBegin', function(name, file){ 
      //rename the incoming file to the file's name 
      file.path = form.uploadDir + "/" + file.name; 
    }) 

    .on('file', function(field, file) { 
     //On file received 
    }) 

    .on('progress', function(bytesReceived, bytesExpected) { 
     //self.emit('progess', bytesReceived, bytesExpected) 

     var percent = (bytesReceived/bytesExpected * 100) | 0; 
     process.stdout.write('Uploading: %' + percent + '\r'); 
    }) 

    .on('end', function() { 


    }); 

form.parse(req); 

La respuesta fue en la página GitHub del módulo ¡todo el tiempo!

+0

¿Por qué está haciendo process.stdout.write() en su lugar console.log()? – qodeninja

Cuestiones relacionadas