3

I am working on Node.js and trying to handle multiple image. I am using following code to upload a single image and then saving the path in string format to the database.

 var multiparty = require("multiparty");
 var form = new multiparty.Form();

 form.parse(req, function(err, fields, files) {
     var img = files.image[0];
     var fs = require('fs');

     fs.readFile(img.path, function(err, data) {
         var path = "/path/to/upload/" + img.originalFilename;

         fs.writeFile(path, data, function(error) {
             if (error) console.log(error);
         });
     });
 })    

Now how to handle multiple image.
Any help will be appreciated!

6
  • @jfriend00 : Can you get the code now? I have edited that. Commented Dec 31, 2015 at 4:56
  • I fixed the code indentation for you. Commented Dec 31, 2015 at 5:02
  • Can i help you by using 'formidable' module instead of 'multiparty' ? Commented Dec 31, 2015 at 6:23
  • Yeah, you are welcome @KethaKavya. Commented Dec 31, 2015 at 6:27
  • I have added the answer using formidable you can try that. Commented Dec 31, 2015 at 6:44

1 Answer 1

2
var express = require('express'),
    app = express(),
    formidable = require('formidable'),
    util = require('util'),
    fs   = require('fs-extra'),
    bodyparser=require('body-parser'),
    qt   = require('quickthumb'),
    path    = require('path');


var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/db');

var Images = require('./model.js');



app.use(qt.static(__dirname + '/'));
app.use(bodyparser());
app.set('view engine','ejs');


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

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

     form.on('field',function(name,value){

      });



  form.on('end', function(fields, files) {

        for(var x in this.openedFiles)
        {
                //Images is my model
                var img = new Images();

                var temp_path = this.openedFiles[x].path;
               /* The file name of the uploaded file */
                var file_name = this.openedFiles[x].name;
                //console.log('file '+file_name);
                img.size = this.openedFiles[x].size;
                img.type = this.openedFiles[x].type;

                /* Location where we want to copy the uploaded file */
                var new_location = 'uploads/';

               console.log(img.nam=new_location+file_name);
               img.save(function(err,imgobj) {
                  if (err)
                   throw err;
               });    
                   //to copy the file into a folder         
                fs.copy(temp_path, new_location + file_name, function(err) {  
                  if (err) {
                    console.log(err);
                  }
               });//fscopy
          }//for loop

    });//form end
res.send('Done!!');

});//post
app.listen(3000);
console.log('started server');
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Ketha Kavya.
This helps me to insert the image in database with different id's. Is it possible to insert all images in array format so i can insert multiple images for one event.
@Kishor This is for multiple image upload.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.